I have a form where the date has to be selected and it has to be passed to an action. But it is not passing as params to that action. I am using jquery datepicker for selecting dates.
View page
<%=f.text_field :due_date%>
Controller page
def create
#task = Task.new(params[:task])
if #task.valid?
#task.due_date=params[:task][:due_date]
#task.save
redirect_to(calenders_path(:date=>#task.due_date,:task_name=>params[:task][:task_name]))
else
render :action=>"new"
end
end
Here only :task_name is passing as params
Please help
Try this:
def create
#task = Task.new(params[:task])
if #task.save
redirect_to(calenders_path(:date=>#task.due_date,:task_name=>#task.task_name))
else
render :action=>"new"
end
end
The due_date param should already be populated by the first line. You might be setting it to null with #task.due_date=params[:task][:due_date].
Related
How can I pass #films to view file films.html.erb and display it on the view file?
def index
#films = Film.select("Title, Year").order('created_at DESC');
#htmldoc = File.read(Rails.root.join('app', 'views','films.html.erb'))
respond_to do |format|
format.html { render html: #htmldoc.html_safe}
end
end
If you want to render the films.html.erb then you don't need to open and read the file, use just the template option for render within your index method.
Adapt a bit your method to something like this:
def index
#films = Film.select('Title, Year').order('created_at DESC')
render template: 'view/films'
end
Note view must match with the name of the folder where's that erb file.
On my rails app I have a form on /addfiles where user can add file path in text boxes and this form is submitted to /remotefiles
I have created a route match '/remotefiles' => 'main#remotefiles'
and function in main controller
def remotefiles
render layout: false
end
and add remotefiles.html.haml in views/main
how can I show these submitted values on remotefiles, I think it can be done with render but not sure how can I use it to pass form values and show them on view.
Is there a way to check form data in ruby on rails just like php print_r function ?
Your form data is coming in via the params hash. The simplest way to respond with it would be
# MainController
def remotefiles
render json: params
end
If your form contained fields named foo and bar, you'll see those as well as some parameters Rails adds:
{
"foo": 1,
"bar": 2,
"controller": "Main",
"action": "remotefiles"
}
If you want to render them into a real template, write the HTML into app/views/main/remotefiles.html.erb. Rails will by default render a template matching your controller and action, or if you want a different one you can instruct Rails to render "path/to/other/template". Your template can access params too, but the more typical way to pass data into them is by setting instance variables in the controller.
# MainController
def remotefiles
#foo = params[:foo]
#bar = params[:bar]
end
# app/views/main/remotefiles.html.erb
<strong>Foo</strong> is <%= #foo %>
<strong>Bar</strong> is <%= #bar %>
Lastly, if you don't actually want to render the form data back to the browser, just inspect it during development, Rails.logger will print it into your server log.
# MainController
def remotefiles
Rails.logger.info(params[:foo], params[:bar])
end
You should read up on how Rails works - the getting started guides are very clear and helpful. Here's the one on rendering.
Right now under my posts controller i have methods
def main
#post = Post.all
end
def show
#post = Post.find(params[:id])
end
I am wondering what is the most basic and simple way to show one post per page and have next and previous links to it. Here i am referencing to main.html.erb . So for instance just have localhost:3000/posts and under that page i can have next and previous links to browse through the post.
Would I be needing some kind of ajax to this? If not then how i can do this using simple activerecord and other elements of rails?
Note: After I click on next, i do need to have the permalink of the post in the url tab.
You could do something like this. Add previous and next method to your model
def previous
posts = where('id > ?', id).limit(1)
if posts.nil?
nil
else
posts.first
end
end
def next
posts = where('id < ?',id).limit(1)
if posts.nil?
nil
else
posts.first
end
end
and then in your view you could do something like this.
unless #post.next.nil? #to show the link to the next post
link_to #post.next
end
unless #post.previous.nil? #to show the link to the next post
link_to #post.previous
end
Anyhow this method isn't that optimized since you will add more two more database queries to get previous and next posts.
You could use the gem will_paginate and set the default per page to 1, as in:
class Post
self.per_page = 1
end
For more information about will_paginate:
https://github.com/mislav/will_paginate
I have a edit form which I have a radio_button that I would like to pass to a controller action and then use it to do a calculation. In the view I have:
<div class="field">
<%= radio_button_tag(:rating_select, "Up") %>
<%= label_tag(:rating_select, "Good.") %>
<%= radio_button_tag(:rating_select, "Down")%>
<%= label_tag(:rating_select, "Bad.")%>
</div>
In the controller I have:
def rating
#post = Post.find(params[:id])
.....
end
def update
#post = Post.find(params[:id])
##rating_select = params[:rating_select]
if #post.rating_select == "Up"
#post.score += 5
elsif #post.rating_select == "Down"
#post.score -= 5
end
......
end
Currently it is ignoring the if statement so the parameter isn't getting set properly. Ideally I would like to just use a temp variable from the view to use in the if statement to decide if I need to add or subtract in the update. But I also have a rating_select field in post if I need to use it also. Thanks.
UPDATE:
Thanks. That makes sense, I changed it to below but it still isn't incrementing or decrementing the score based on the radio box. So it seems it isn't getting the rating_select?:
def update
#post = Post.find(params[:id])
if params[:rating_select]=="Up"
#post.score += 5
elsif params[:rating_select]=="Down"
#post.score -= 5
end
respond_to do |format|
....
UPDATE2:
Finally figured it out, used another model Ratings to store association. I used the before_save in the Post model and it allowed me to do the calculation and save. What a headache.
before_save :set_rating
def set_rating
if self.rating.rating_select=="Up"
rating.score += 5
elsif self.rating.rating_select=="Down"
rating.score -= 5
end
end
Well, first off, in the code you're showing the post loaded in your update action is not receiving the params from your view.
Your code for an update action should typically look like this:
def update
#post = Post.find(params[:id])
if #post.update_attributes(params[:post])
... do stuff ...
else
render :edit, :alert => 'Unable to update post.'
end
end
Second, since you're using form_tag helper and not form_for, then you're not getting the params all setup for your model (ie. nested under params[:post]). So, in this case, your rating_select option is just a value by itself, which you can test for like this:
if params[:rating_select]=="Up"
...
else
...
end
The big thing to understand from your code is #post doesn't know anything about params[:rating_select], even if you used #post.update_attributes(params[:post]), because radio_button_tag as you have it set up is not building a hash of post attributes, it's just a standalone field.
I hope that makes sense, if you don't understand please leave comments and I'll try to explain more.
I'd like to delegate to the Show action from the Index action if an id was passed. I can't seem to get it to work, what am I doing wrong here?
require 'ramaze'
require 'slim'
class UsersController < Ramaze::Controller
engine :slim
def index(id=nil)
if id
render_full "/users/show/#{id}" #id was passed, "show" the item
end
#alright just render the "index.slim" here...
end
def show(id)
u=User[id] #ORM stuff...
end
end
Ramaze.start
You have to call return render_full(...) opposed to just render_full(), without this the code below it will be executed regardless of whether or not an ID was specified.
You can call return show(id) instead of render_full "/users/show/#{id}"