如何在formtastic中预先检查复选框 [英] How to pre-check checkboxes in formtastic
问题描述
我正在尝试设置一个表单...用户可以有很多帖子,每个帖子可以有很多人在看.
I have a form I'm trying to set up ... Users can have many posts, and each post can have many people watching it.
Watch 模型以多态方式设置为可观看",因此它可以应用于不同类型的模型.它有一个 user_id、watchable_id、watchable_type 和时间戳作为属性/字段.
The Watch model is set up polymorphically as 'watchable' so it can apply to different types of models. It has a user_id, watchable_id, watchable_type and timestamps as attributes/fields.
这只是为了当人们对帖子发表评论时,观看帖子的用户可以收到一封关于它的电子邮件.
This is soley so that when people comment on a post, users watching the post can get an email about it.
我想做的是向用户显示他们可以在每个帖子上标记的用户列表,这没有问题.这是我现在用的
What I'm trying to do is show the user a list of users that they can tag on each post, which is not problem. This is what I'm using right now
<% semantic_form_for @update do |f| %>
<%= f.input :subject, :input_html => { :class => 'short' } %>
<%= f.input :site, :include_blank => false, :input_html => { :class => 'short' } %>
<label>Tag Users (they will get notified of this update)</label>
<%= f.input :user, :as => :check_boxes, :label => ' ', :wrapper_html => { :class => "radiolist clearfix" }, :for => :watch, :name => "Watch" %>
<%= f.input :note, :label => 'Update'%>
<% f.buttons do %>
<%= f.commit_button :button_html => { :class => 'submit' } %>
<% end %>
<% end %>
这个问题是,当你去编辑更新/帖子时......所有复选框都被预先选中......我希望它只预先检查当前正在观看帖子的用户.
The problem with this, is that when you go to edit an update/post ... all the checkboxes are prechecked ... I want it to pre-check only users who are currently watching the post.
进一步澄清......这是我现在让它做我想做的事情的hacky方式
To further clarify ... this is the hacky way I'm getting it to do what I want right now
<ul class="radiolist clearfix">
<% @users.each do |user| %>
<li>
<%= check_box_tag 'user_ids[]', user.id, @watches.include?(user.id) ? true : false -%>
<%= h user.name -%>
</li>
<% end %>
</ul>
@watches 只是一个用户 ID 数组
where @watches is just an array of user ids
@watches = @update.watches.map{|watcher| watcher.user_id}
推荐答案
对于有同样问题的其他人:
For anyone else having the same issue:
<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
这篇关于如何在formtastic中预先检查复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!