如何预先检查formtastic中的复选框

我有一个表单,我正在设置…用户可以有很多post,每个post都可以有很多人观看。

Watch模型以多态forms设置为“可观察”,因此它可以应用于不同类型的模型。 它具有user_id,watchable_id,watchable_type和时间戳作为属性/字段。

这是非常好的,所以当人们评论post时,观看post的用户可以收到有关它的电子邮件。

我要做的是向用户显示他们可以在每个post上标记的用户列表,这不是问题。 这就是我现在正在使用的

http://pastie.org/940421

  { :class => 'short' } %>  false, :input_html => { :class => 'short' } %>   :check_boxes, :label => ' ', :wrapper_html => { :class => "radiolist clearfix" }, :for => :watch, :name => "Watch" %>  'Update'%>   { :class => 'submit' } %>   

这个问题是,当你去编辑更新/发布时…所有的复选框都是预先检查的…我希望它只预先检查当前正在观看post的用户。

为了进一步澄清…这是我现在想要做的事情的hacky方式

 

其中@watches只是一个用户ID数组

 @watches = @update.watches.map{|watcher| watcher.user_id} 

对于其他有同样问题的人:

 <%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %> 

对于多个复选框,这种方式:

 <%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %> 

在渲染表单之前,在控制器中将布尔属性的值设置为“true”。 这应该使Formtastic默认为“已选中”复选框。

如果您需要复选框的状态来反映值:some_input

 <%= form.input :some_input, :as => :boolean, :input_html => { :checked => :some_input? } %> 

在你的模型..

 def some_input? self.some_input ? true : false end