如何在 formattastic 中预先检查复选框

发布于 2024-08-30 07:54:34 字数 1578 浏览 6 评论 0原文

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

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}

I have a form I'm trying to set up ...
Users can have many posts, and each post can have many people watching it.

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

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 => ' ', :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.

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>

where @watches is just an array of user ids

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如若梦似彩虹 2024-09-06 07:54:34

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

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

For anyone else having the same issue:

<%= f.input :some_input, :as => :boolean, :input_html => { :checked => 'checked' } %>
与他有关 2024-09-06 07:54:34

对于多个复选框,这样:

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

For multiple check boxes, this way:

<%= f.input :tags, :as => :check_boxes, :collection => some_map.collect { |c| [c[:name], c[:id], {:checked=> tag_ids.include?(c[:id])}] } %>
痴意少年 2024-09-06 07:54:34

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

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

模型中 :some_input 的值..

def some_input?
  self.some_input ? true : false
end

If you need the state of the checkbox to reflect the value of :some_input

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

In your model..

def some_input?
  self.some_input ? true : false
end
泼猴你往哪里跑 2024-09-06 07:54:34

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

Set the boolean attribute's value to 'true' in the controller before your render the form. That should make Formtastic default the checkbox to 'checked'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文