使用复选框/错误数量的参数rails 4以一种形式更新多个记录 [英] update multiple records in one form using checkboxes / wrong number of arguments rails 4
问题描述
我一直在教程/论坛中摸索,试图弄清楚如何使用复选框更新多种方法.
I've been bumbling my way through tutorials/forums in an attempt to figure out how update multiple methods using checkboxes.
我需要能够列出我的事件",并在日期可用的情况下选中或取消选中它们.我想以一种形式进行此操作.我知道这是可能的,但是在无法正常工作之后,我终于来了.
I need to be able to list my "events" and check or uncheck them if the date is available. I'd like to do this on one form. I know this is possible, but I'm finally coming here after failing to get it working.
使用我发布的代码,我收到错误的参数数量,2对1"错误.
With the code I've posted I'm getting "Wrong Number of Arguments, 2 for 1" error.
我尝试了以下信息来源: http://railscasts.com/episodes/52-update-through-checkboxes http://discuss.codeschool. io/t/surviving-apis-with-rails-posting-multiple-records/4776/4 http://railscasts.com/episodes/165-edit-multiple-revised
I've tried these info sources: http://railscasts.com/episodes/52-update-through-checkboxes http://discuss.codeschool.io/t/surviving-apis-with-rails-posting-multiple-records/4776/4 http://railscasts.com/episodes/165-edit-multiple-revised
我在这里
routes.rb
routes.rb
resources :events do
collection do
put :verified
post :make_events
end
end
events_controller.rb
events_controller.rb
def verified
if Event.update_all(["available", true], :id => params[:event_ids])
redirect_to step2_path(:pid => @project.id, :u => current_user.id)
else
end
end
show.html.erb
show.html.erb
<%= form_tag verified_events_path(:pid => @project.id ), method: :put do %>
<table class="table-event-dates">
<thead>
<tr>
<th> </th>
<th> </th>
</tr>
</thead>
<tbody>
<tr><% @these_events.each do |event| %>
<td><%= check_box_tag "event_id[]", event.id, :value => event.available %></td>
<td><label> <%= event.date.strftime("%A, %b. %d %G") %></label></td>
</tr>
</tbody>
<% end %>
</table>
</div><br><!-- panel-body -->
<div class="panel-footer2">
<div class="row">
<%= submit_tag 'Verify Dates', :class => 'btn btn-green btn-lg btn-block' %>
<% end %>
推荐答案
可能应该采用老式风格:
Probably it should work in old-fashioned style:
Event.update_all("available = 1", ["id in (?)", params[:event_ids]])
也许available = true
或'true'
,我不确定.或者:
perhaps available = true
or 'true'
, i'm not sure. Or:
Event.update_all(["available", true], ["id in (?)", params[:event_ids]])
但是,也许您应该清除参数.检查它们的格式是否正确(1, 2, 4
..).
However, maybe you should clear params. Check that they are in correct form (1, 2, 4
..).
您也可以尝试以下方法:
Also you could try this:
Event.where(id: params[:event_ids]).update_all(available: true)
这篇关于使用复选框/错误数量的参数rails 4以一种形式更新多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!