开发者

Pre-checking collection of model objects used as check boxes with Formtastic

开发者 https://www.devze.com 2023-03-09 07:29 出处:网络
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 %>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消