我正在尝试为表单创建多个提交按钮,但我不断收到此错误:
Couldn't find User with id=edit_individual
发现于:
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end
我的控制器页面:
def edit_individual
if User.find_by_id (params[:user_ids])
if params[:Delete_multiple]
@users = User.find(params[:user_ids])
@users.each do |user|
user.destroy
end
redirect_to :back, notice: 'All selected users were successfuly deleted'
else
@users = User.find(params[:user_ids])
end
else
redirect_to :back, alert: 'No users were selected for editing'
end
end
def update_individual
@users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
if @users.empty?
redirect_to :back, notice: "Users updated"
else
render :action => "edit_individual"
end
end
我的索引页:
<%= form_tag edit_individual_users_path do %>
<table class="table table-hover">
<thead>
<tr>
<th></th>
<th><%= sort_link @q, :firstName, "First name" %></th>
<th><%= sort_link @q, :lastName, "Last name" %></th>
<th><%= sort_link @q, :registrationNumber, "Registration number" %></th>
<th><%= sort_link @q, :email, "Email" %></th>
<th></th>
</tr>
</thead>
<tbody>
<% @users.each do |user| %>
<% if (user.archive == false) %>
<tr>
<td><%= check_box_tag "user_ids[]", user.id %></td>
<td><%= user.firstName %></td>
<td><%= user.lastName %></td>
<td><%= user.registrationNumber %></td>
<td><%= user.email %></td>
<td><%= link_to 'View', user, class: 'btn btn-info btn-mini', title: 'View users\'s information' %></td>
<td><%= link_to 'Edit', edit_user_path(user), class: 'btn btn-warning btn-mini', title: 'Edit users\'s information', method: 'get' %></td>
<td><%= link_to 'Delete', user, class: 'btn btn-danger btn-mini', title: 'Delete user', method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
<p><%= select_tag :field, options_for_select([["All Fields", ""], ["First name", "firstName"], ["Last name", "price"], ["Registration number", "registrationNumber"], ["Email", "email"]]) %></p>
<p><%= submit_tag "Edit checked", name: 'Edit_multiple' %></p>
<p><%= submit_tag "Delete checked", name: 'Delete_multiple', data: { confirm: 'Are you sure you want to delete multiple users?' } %></p>
<% end %>
edit_individual页面:
<% form_tag update_individual_users_path, :method => :put do %>
<% for product in @products %>
<% fields_for "users[]", users do |f| %>
<h2><%=h user.name %></h2>
<%= render :partial "fields", :locals => { :f => f, :user => user } %>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
路线档案:
resources :users do
get 'archive', on: :collection
post 'edit_individual', on: :collection
put 'update_individual', on: :collection
end
删除部分工作正常。此外,编辑部分实际上编辑了该字段,但显示该错误(因此它不会重定向回索引页面,使其看起来很糟糕)。
任何人都可以帮我解决这个问题吗?我花了很多时间同时拥有edit
和delete
所以如果我不得不删除编辑部分会非常糟糕。
编辑1:此外,如果我正在编辑3个用户并单击提交按钮,只允许2/3通过(由于验证),则没有错误(我只是被重定向回edit_individual.html
完成最后一个用户)然后如果我再次点击提交并且一切都通过,我就会收到错误。
答案 0 :(得分:0)
我确实设法找到了什么问题。 问题在于:
def update_individual
@users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
if @users.empty?
redirect_to :back, notice: "Users updated"
else
render :action => "edit_individual"
end
end
而不是:
redirect_to :back
应该是:
redirect_to users_path
或者我认为的任何其他固定路径。有没有办法保持&#34; redirect_to:back&#34;,或者我是否必须为将使用它的其他视图(在同一个控制器中)创建新方法?
答案 1 :(得分:0)
它不应该运行,因为你没有params[:id]
如果您使用before_filter,那么
before_filter: set_user except: [:update_individual, :edit_individual]
def set_user
@user = User.find(params[:id])
end
您在update_individual
edit_individual
的地方