【问题标题】:Rails 5: stuck in Edit action, Update not workingRails 5:卡在编辑操作中,更新不起作用
【发布时间】: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 是在 &lt;form class&gt; 标记之前还是之后都没有关系。看来我必须把它放在&lt;form class&gt; 标签之前。所以现在不,我在屏幕上发现了主要错误: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


【解决方案1】:

form_for ([:common, @role], :html =&gt; { method: 'POST' }) 应该可以工作

【讨论】:

  • 它给了我这个错误:syntax error, unexpected keyword_ensure, expecting end-of-input 尽管我在表格末尾放了``。
  • 但是没有变化 - 仍然停留在编辑操作中
  • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 2017-02-25
  • 2014-03-17
相关资源
最近更新 更多