我已经遇到了一些简单的事情,我已经尝试了很多不能工作的事情。这就是我所拥有的。 在我的应用中,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]]
当我按下&#34;保存更改&#34;按钮,我看到了:
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
看起来没有传递&#34;角色&#34; =&gt; {... 并且在令牌之后它显示我尝试更新的参数。据我所知,这可能是为什么更新没有发生。
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>
标签之前。所以现在没有我在screent上发现主要错误:param is missing or the value is empty: role
任何想法为什么?
更新4
好的,现在我已经找到了主要问题。基本上Enums根本没有更新。由于我的表单仅由枚举组成,因此不会传递也不会更新。现在我必须找出,我必须如何正确保存/更新枚举。欢迎任何提示!
答案 0 :(得分:0)
form_for ([:common, @role], :html => { method: 'POST' })
应该有效