gpt4 book ai didi

ruby-on-rails - 无法以充满 check_box_tag 字段的形式获取 Update_all 在 Rails 4 中工作

转载 作者:数据小太阳 更新时间:2023-10-29 08:46:52 25 4
gpt4 key购买 nike

我在使用简单的 bool 复选框更新记录列表时遇到了严重的问题。

我需要能够列出我的“事件”并在日期可用时选中或取消选中它们。我想在一种形式上做到这一点。我知道这是可能的,但在无法正常工作后我终于来到了这里。

当我提交表单时,它遵循正确的 if: redirect 路径,就好像更新已成功...但记录保持不变并且 updated_at 也保持未链接。

我试过这些信息来源: 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

这是我的位置

路线.rb

resources :events do
collection do
put :verified
post :make_events
end
end

events_controller.rb

def verified
if Event.where(id: params[:event_ids]).update_all(available: true)
redirect_to step2_path(:pid => @project.id, :u => current_user.id)
else
redirect_to show_events_path(:pid => @project.id, :u => current_user.id)
end
end

显示.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 %>

我已经坚持了一段时间。

我发布了一个类似的问题,但修复与错误无关,也没有解决错误……但是,它确实解决了另一个错误。很抱歉发了两次,但这似乎是我同一个项目中的一个不同问题。

update multiple records in one form using checkboxes / wrong number of aruguments rails 4

最佳答案

所以我的问题的关键原来是没有列出 bool 变量和我必须为未选中的复选框指定一个值的组合。

这救了我:

Controller :

def verfied

Event.where(id: params[:event_ids]).update_all({:available => true})
Event.where.not(id: params[:event_ids]).update_all({:available => false})
return redirect_to step2_path(:pid => @project.id, :id => @project.id, :u => current_user.id, :t => @project.template_id)

else
redirect_to event_edit_path(:pid => project_id, :u => current_user.id)


end

这是我的表单的最终代码:

 <div class="form-wrap">
<%= 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_ids[]", event.id, event.available %><%= hidden_field_tag :updated_at, Time.now %></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' %>

</div>
</div>

</div><!-- panel -->


</div><!-- col-md-6 --><!-- col-md-6 -->

<% end %>

关于ruby-on-rails - 无法以充满 check_box_tag 字段的形式获取 Update_all 在 Rails 4 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25315823/

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