Pre-checking collection of model objects used as check boxes with Formtastic
So I've got some objects that I'm passing as a collection:
@things = Thing.all
And in my view:
<%= f.input :things, :collection => @things, :as => :check_boxes %>
开发者_开发百科
Is there any way to specify which of the objects should already be checked when the view is loaded?
Thanks!
In your controller's "new" action, you want to initialise the form object with the values you want by default:
def new
@post = Post.new
@post.things = [@one_thing, @another_thing]
end
This can also be done as an after_initialize
hook in the model itself.
精彩评论