Reputation:
So i'm hitting my head on this. I want to have a checkbox that sends Time.now to my database under the featured column.
I know how to pass params using check_box_tag and that seems to pass as seen below:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"24aw2xi68XsgMrU28is3wcXkNdhLlgVHhZBuJdd9984=", "article"=>{"body"=>"oops", "published"=>"0", "headline"=>"headlinez", "input_slug"=>"weee", "article_position"=>"5", "issue_id"=>"1", "section_id"=>"1", "author_id"=>"", "flag"=>"0"}, "commit"=>"Save Article", "featured"=>"2014-01-25 19:28:57 -0800", "id"=>"weee"}
But how can i get that to pass from WITHIN article? check_box_tag seems to not be liked by forms, so how do you do it?
Here is my form:
<div class="field">
<label>Featured Article?</label>
<%= check_box_tag :featured, @time %>
</div>
Upvotes: 0
Views: 1580
Reputation: 18070
To move the parameter to be within the article hash, you can name the check_box_tag something like this
<%= check_box_tag 'article[featured]', @time %>
EDIT - Adding more to this answer since you're using form_for
You can do this sort of thing, since you have an @article and a 'featured' method...
<%= f.check_box :featured, {}, Time.now %> # As you wrote in your answer
But, you can also do this, which is a little more Rails standard...
Change 'featured' to be featured_at (this is the more standard way of indicating a datetime), and add a 'featured' attr_access to article.
class Article < ActiveRecord::Base
attr_accessor :featured
def featured
!!featured_at
end
def featured=(value)
if featured_at.present? && value.blank?
self.featured_at = nil
elsif featured_at.blank? && value.present?
self.featured_at = Time.now
end
end
And then, you can submit a boolean via the form for featured
, instead of a datetime.
= f.check_box :featured, {}, 'true', ''
This is less prone to any sort of hacking. That may not matter here, just a good practice.
Upvotes: 0
Reputation:
I was finally able to do this the following way
<%= f.check_box :featured, {}, Time.now %>
Upvotes: 0