【发布时间】:2017-02-26 12:11:42
【问题描述】:
我被一种简单的事情困住了,我尝试了很多不起作用的事情。这就是我所拥有的。 在我的应用程序 current_user 需要更新属于 current_user 公司的用户的角色。我可以通过编辑操作,在适当的选择特定用户角色的情况下,但是我无法做更新操作 - 它始终保持在编辑动作。
这就是我在 /models/role.rb 中的内容:
class Role < ApplicationRecord
belongs_to :user, optional: true, inverse_of: :roles
accepts_nested_attributes_for :user
enum general: { seller: 1, buyer: 2, seller_buyer: 3}, _suffix: true
enum dashboard: { denied: 0, viewer: 1, editer: 2, creater: 3, deleter: 4}, _suffix: true
# other columns for roles follow #
/models/user.rb 看起来像这样:
#User has roles
has_many :roles
has_many :company_user_roles, through: :companies, source: :user_roles
accepts_nested_attributes_for :roles, reject_if: proc { |attributes| attributes[:name].blank? }
# User has many companies
has_many :accounts, dependent: :destroy
has_many :companies, through: :accounts
在 /controllers/common/roles_controller.rb 我有这个:
class Common::RolesController < ApplicationController
def edit
@role = Role.find(params[:id])
end
def update
@role = Role.find(params[:id])
if @role.update_attributes(role_params)
flash[:success] = "Role updated!"
redirect_to dashboard_path
else
render 'edit'
end
private
def role_params #at the end ID of user to whom belongs role is stored
params.require(:role).permit(:general, :dashboard, //..other role table columns..// , :user_id)
end
在 /views/common/roles/edit.html.erb 我有这个:
<%= form_for ([:common, @role]) do |f| %>
<%= f.select :general, Role.generals.map { |key, value| [key.humanize, key] } %>
<%= f.select :dashboard, Role.dashboards.map { |key, value| [key.humanize, key] } %>
<%= f.submit "Save changes" %>
<% end %>
当我打开 /common/roles/1/edit 我看到这个:
Started GET "/common/roles/1/edit" for 194.8.16.19 at 2016-10-17 13:08:17 +0000
Processing by Common::RolesController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Role Load (0.5ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
当我按下“保存更改”按钮时,我看到了这个:
Started GET "/common/roles/1/edit?utf8=%E2%9C%93&_method=patch&authenticity_token=QoCCvXkM2%2B77ZyO0npTBKv1PKTQdkFjkLLbIgmdSXN8uli1ElLBfwHD6GXVTA%2Fa65cQPVPCqJNSkF0d8l5SSgw%3D%3D&general=seller_buyer&dashboard=editer&rights=editer&commit=Save+changes" for 194.8.16.19 at 2016-10-17 13:10:54 +0000
Processing by Common::RolesController#edit as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"QoCCvXkM2+77ZyO0npTBKv1PKTQdkFjkLLbIgmdSXN8uli1ElLBfwHD6GXVTA/a65cQPVPCqJNSkF0d8l5SSgw==", "general"=>"seller_buyer", "dashboard"=>"editer", "rights"=>"editer", "commit"=>"Save changes", "id"=>"1"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Role Load (0.4ms) SELECT "roles".* FROM "roles" WHERE "roles"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
或者像这样:
--- !ruby/object:ActionController::Parameters
parameters: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: "✓"
_method: patch
authenticity_token: QoCCvXkM2+77ZyO0npTBKv1PKTQdkFjkLLbIgmdSXN8uli1ElLBfwHD6GXVTA/a65cQPVPCqJNSkF0d8l5SSgw==
general: seller_buyer
dashboard: editer
rights: editer
commit: Save changes
controller: common/roles
action: edit
id: '1'
permitted: false
看起来它没有传递 "role"=> {... 并且在令牌之后它显示了我尝试更新的参数。据我了解,这可能是更新没有发生的原因。
角色 routes.rb 看起来像这样:
namespace :common do
resources :companies
resources :roles
end
角色的索引和编辑操作有效,所有操作(包括更新)都适用于公司。
我对这个错误有点绝望,因为我尝试了很多东西,但没有任何效果。 请问如何解决此问题以使更新操作正常工作?感谢您提供任何可能存在错误的提示。
更新
我可以毫无问题地在控制台中手动更新角色 - 按 ID 查找角色并更新任何列。仍然没有运气 - 在f.submit 之后停留在编辑操作中。
更新 2
似乎所有有路线的东西都很好:
common_roles GET /common/roles(.:format) common/roles#index
POST /common/roles(.:format) common/roles#create
new_common_role GET /common/roles/new(.:format) common/roles#new
edit_common_role GET /common/roles/:id/edit(.:format) common/roles#edit
common_role GET /common/roles/:id(.:format) common/roles#show
PATCH /common/roles/:id(.:format) common/roles#update
PUT /common/roles/:id(.:format) common/roles#update
DELETE /common/roles/:id(.:format) common/roles#destroy
到目前为止,不知道为什么仍然会发生错误……据我所知,这不是因为路线错误。
更新 3
好的,我已经弄清楚了为什么编辑操作会卡住 - 因为我认为 form_for 是在 <form class> 标记之前还是之后都没有关系。看来我必须把它放在<form class> 标签之前。所以现在不,我在屏幕上发现了主要错误:param is missing or the value is empty: role 有什么想法吗?
更新 4
好的,现在我发现了主要问题。基本上枚举根本没有更新。由于我的表单仅由枚举组成,因此不会传递也不会更新。现在我必须找出如何正确保存/更新枚举。欢迎任何提示!
【问题讨论】:
-
问题在于方法:
form_for应该使用 POST 方法生成一个表单(单击“保存更改”时应该会看到“在 [...] 上开始 POST”) -
@user3033467 是的,我知道,但是我有相同的 form_for "Companies"(例如,/common/companies/1/edit),我可以毫无问题地进行更新。这让我很困惑。请问我怎么去 POST 呢?
-
您不应该在方法名称和它们的左括号之间使用空格。
form_for (应该是form_for(。 -
@meagar,好的,已更改,但错误相同。我尝试了 user3033467 解决方案,但没有任何改变 - 我遇到过同样的旧错误:(
标签: ruby-on-rails ruby views controllers