加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何预先检查formtastic中的复选框

发布时间:2020-12-16 20:38:50 所属栏目:百科 来源:网络整理
导读:我有一个表单我正在设置… 用户可以有很多帖子,每个帖子可以让很多人看到它. 手表模型以多视角设置为“可监视”,因此可应用于不同类型的模型.它具有user_id,watchable_id,watchable_type和timestamp作为属性/字段. 这是soley,所以当人们对帖子发表评论时,观
我有一个表单我正在设置…
用户可以有很多帖子,每个帖子可以让很多人看到它.

手表模型以多视角设置为“可监视”,因此可应用于不同类型的模型.它具有user_id,watchable_id,watchable_type和timestamp作为属性/字段.

这是soley,所以当人们对帖子发表评论时,观看帖子的用户可以收到关于它的电子邮件.

我想要做的是向用户显示他们可以在每个帖子上标记的用户列表,这是没有问题的.这是我现在正在使用的

http://pastie.org/940421

<% 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 => '&nbsp;',: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 %>

这个问题是,当你去编辑一个更新/发布…所有的复选框被预先检查…我希望它预先检查正在观看这个职位的用户.

要进一步澄清…这是我现在正在做我想要的那种奇怪的方式

<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的数组

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

解决方法

对于任何有同样问题的人:
<%= f.input :some_input,:as => :boolean,:input_html => { :checked => 'checked' } %>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读