gpt4 book ai didi

ruby-on-rails - 如何预先检查复选框形式

转载 作者:行者123 更新时间:2023-12-03 14:42:49 26 4
gpt4 key购买 nike

我有一个要设置的表格...
用户可以有很多帖子,每个帖子可以有很多人在看它。

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

这是唯一的,因此当人们对帖子发表评论时,观看该帖子的用户可以获得有关该帖子的电子邮件。

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

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' } %>

关于ruby-on-rails - 如何预先检查复选框形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2734951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com