预先检查模型对象的集合,这些对象用作带有Formtastic的复选框。

时间:2021-12-05 15:57:32

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!

谢谢!

1 个解决方案

#1


1  

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.

这也可以作为模型本身的after_initialize钩子来完成。

#1


1  

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.

这也可以作为模型本身的after_initialize钩子来完成。