如何更新多条记录 Rails 3 中复选框的属性?

发布于 2024-11-14 21:51:52 字数 2147 浏览 4 评论 0原文

我有一个属性可以将页面添加到我的网站导航(它是一个布尔值),并且我希望能够同时从复选框打开和关闭页面。我尝试遵循这个railscast: http://railscasts.com/episodes/52-update-through-checkboxes但我遇到了两个问题。

一 - 我希望复选框显示“导航栏”选项的当前状态。

和两个 - 我不知道如何更新导航栏字段。

这是我的控制器:

  def nav
    Section.update_all([:navbar => :params[:navbar]], :id =>params[:section_ids])
    flash[:success] = "Sections were added to navbar"
    redirect_to(admin_sections_path)
  end

和我的视图:

<%= form_tag nav_admin_sections_path, :method => :put do %>
  <ol id="section_list" class="records_list">
  <% @sections.each do |section| %>
    <li id="section_<%= section.id %>">
    <table>
      <tr class="handle">
        <td class="title link_icon directory_link"><%= section.name %></td>
        <td class="option"><%= check_box_tag "section_ids[]", section.id %></td>
        <td class="action"><%= link_to 'Edit', edit_admin_section_path(section), :class=>"link_icon edit_link"   %></td>
        <td class="action">
          <% if section.has_bio == false %>
          <%= link_to 'Destroy', admin_section_path(section), :confirm => 'Are you sure?', :method => :delete, :class=>"link_icon delete_link"   %>
          <% end %>
        </td>
      </tr>
      </table>

    </li>
  <% end %>
  </ol>

  <ol>
    <li class="submit">
      <%= submit_tag %>
    </li>
  </ol>
<% end %>

由于我正在做一些 jquery-ui 可排序的事情,所以该表嵌套在列表项中。

无论如何,我需要该复选框来显示 :navbar 的当前状态,并且我需要能够更新它们。现在,如果我尝试更新它们,我会收到此错误:

    can't convert Symbol into Integer
app/controllers/admin/sections_controller.rb:95:in `[]'
app/controllers/admin/sections_controller.rb:95:in `nav'

在我的控制器中的这一行”

Section.update_all([:navbar => :params[:navbar]], :id =>params[:section_ids])

所以我想我没有正确地将 check_box 状态传递到我的控制器中。

I have an attribute to add a page to my site's navigation (its a boolean) and I want to be able to turn pages on and off from check boxes all at once. I tried following this railscast:
http://railscasts.com/episodes/52-update-through-checkboxes but Im running into two problems.

One - I want the check boxes to show the current state of the "navbar" option.

and two - Im not sure how to update the navbar field.

Here is my controller:

  def nav
    Section.update_all([:navbar => :params[:navbar]], :id =>params[:section_ids])
    flash[:success] = "Sections were added to navbar"
    redirect_to(admin_sections_path)
  end

and my view:

<%= form_tag nav_admin_sections_path, :method => :put do %>
  <ol id="section_list" class="records_list">
  <% @sections.each do |section| %>
    <li id="section_<%= section.id %>">
    <table>
      <tr class="handle">
        <td class="title link_icon directory_link"><%= section.name %></td>
        <td class="option"><%= check_box_tag "section_ids[]", section.id %></td>
        <td class="action"><%= link_to 'Edit', edit_admin_section_path(section), :class=>"link_icon edit_link"   %></td>
        <td class="action">
          <% if section.has_bio == false %>
          <%= link_to 'Destroy', admin_section_path(section), :confirm => 'Are you sure?', :method => :delete, :class=>"link_icon delete_link"   %>
          <% end %>
        </td>
      </tr>
      </table>

    </li>
  <% end %>
  </ol>

  <ol>
    <li class="submit">
      <%= submit_tag %>
    </li>
  </ol>
<% end %>

The table is nested in the list item because of some jquery-ui sortable stuff Im doing.

Anyway I need that check box to show the current state of :navbar AND I need to be able to update them. Right now if I try to update them I get this error:

    can't convert Symbol into Integer
app/controllers/admin/sections_controller.rb:95:in `[]'
app/controllers/admin/sections_controller.rb:95:in `nav'

on this line in my controller"

Section.update_all([:navbar => :params[:navbar]], :id =>params[:section_ids])

So I guess Im not passing the check_box state into my controller correctly.

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

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

发布评论

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

评论(1

宛菡 2024-11-21 21:51:52
  1. 正确选中该复选框。只需添加布尔值作为第三个参数即可。

    <%= check_box_tag "section_ids[]",section.id,section.navbar %>;
    
  2. 更新导航栏字段您必须设置导航栏打开和关闭的两个部分。

    ids = [*params[:section_ids]] + [0] # 确保在没有选择导航栏时它可以工作
    Section.update_all({:navbar => true}, {:id => ids})
    Section.update_all({:navbar => false}, "sections.id NOT IN (#{ ids.join(',') })")
    

编辑

在第二个 update_all 中需要双引号而不是单引号。更新了上面的代码。

编辑2

join 内的逗号括在引号中

  1. Have the checkbox check properly. Just add the boolean as the third argument.

    <td class="option"><%= check_box_tag "section_ids[]", section.id, section.navbar %></td>
    
  2. update the navbar field You'll have to set both the sections where navbar is on and off.

    ids = [*params[:section_ids]] + [0] # makes sure it works when no navbars are selected
    Section.update_all({:navbar => true}, {:id => ids})
    Section.update_all({:navbar => false}, "sections.id NOT IN (#{ ids.join(',') })")
    

EDIT

Needs double quotes instead of single quotes in second update_all. Updated the above code.

EDIT 2

Enclosing the comma inside the join in quotes

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