如何更新多条记录 Rails 3 中复选框的属性?
我有一个属性可以将页面添加到我的网站导航(它是一个布尔值),并且我希望能够同时从复选框打开和关闭页面。我尝试遵循这个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正确选中该复选框。只需添加布尔值作为第三个参数即可。
更新导航栏字段您必须设置导航栏打开和关闭的两个部分。
编辑
在第二个 update_all 中需要双引号而不是单引号。更新了上面的代码。
编辑2
将
join
内的逗号括在引号中Have the checkbox check properly. Just add the boolean as the third argument.
update the navbar field You'll have to set both the sections where navbar is on and off.
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