-
Notifications
You must be signed in to change notification settings - Fork 124
-
Notifications
You must be signed in to change notification settings - Fork 124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
It is needed example with checkboxes #9
Comments
@sandiktopas it may take me a little bit to update the demo app, but in the meantime here are some pointers that should get you going: I assume that you want to use a checkbox to filter your records on a boolean attribute. For this example, let's use
Then you need to create a scope: scope :with_expired_only, lambda { |flag|
return nil if 0 == flag # checkbox unchecked
where(is_expired: true)
} And make sure to add the new scope to your model's The detailed documentation is always a great resource to help you solve any remaining issues. |
Suppose you wanted checkboxes but were not filtering on a boolean attribute? Suppose I had a books index and I wanted to supply a (dynamic) set of checkboxes to filter on authors (one checkbox per author so that I can get all books written by one of the checked authors). This is somewhat similar to what is mentioned in the docs about using an array of country id's, but there is no view example. If I have a scope I got it to work by doing it without without the form_for's checkbox helper, i.e.:
And then in the controller:
This seems kind of cumbersome. I wonder if anyone has a better suggestions but if not I hope at least this helps someone else trying to do this. |
For Rails 4 how about using the f.collection_check_boxes :with_author_ids, Author.all.map{|a| a.name, a.id}, :last, :first |
You can also do it this way with the <% Author.all.each do |a| %>
<%= f.check_box(:with_author_ids, {multiple: true}, a.id, false %>
<%= a.name %>
<% end %> NOTE: I think you want to stick with using the form builder object |
My need is slightly different. How do I make checkbox work with predefined values. For instance, I have a "hobby" field with values like Reading, Travelling, Dancing... I want users to select and filter as many hobbies as possible. How do I present this on the index view? Thanks! |
Could someone give example with checkboxes?
The text was updated successfully, but these errors were encountered: