[英]ParameterMissing - param is missing or the value is empty: user
I need to change the user type. 我需要更改用户类型。 Doing through STI.
通过STI完成。
Clicking the link passes the parameter :type. 单击该链接将传递参数:type。
My models: 我的模特:
class User < ApplicationRecord
class Admin < User
class Seller < User
class Guest < User
Views 查看
tbody
- @users.each do |user|
tr
td = user.email
td = user.type
td = link_to "admin", edit_user_path(user, type: 'Admin')
td = link_to "guest", edit_user_path(user, type: 'Guest')
td = link_to "seller", edit_user_path(user, type: 'Seller')
routes.rb 的routes.rb
resources :users
resources :admins, controller: :users, :type => "Admin"
resources :guests, controller: :users, :type => "Guest"
resources :sellers, controller: :users, :type => "Seller"
Error 错误
ActionController::ParameterMissing in UsersController#update
param is missing or the value is empty: user
Parameters:
{"_method"=>"patch", "authenticity_token"=>"56bC4iWSsYd+VrN7RV0NCMj4YD+fIMQKb8uKKC9dUkMtOORAilwPlk3oAgT2L8j5qMsH4tNdzy6zbdL5nVVi7Q==", "type"=>"Admin", "id"=>"3"}
controller 调节器
def update
if current_admin
if @user.update(user_params)
redirect_to users_path
else
render 'edit'
end
end
end
private
def user_params
params.require(:user).permit(:type)
end
Why this error? 为什么这个错误? action expects that one parameter :the user, but it is not?
action期望一个参数:用户,但它不是?
require
ensures that a parameter is present. require
确保存在参数。 If it's present, returns the parameter at the given key, otherwise raises an ActionController::ParameterMissing error
. 如果它存在,则返回给定键的参数,否则引发
ActionController::ParameterMissing error
。
You are requiring user
parameter but it's missing from the params. 您需要
user
参数,但它们在参数中缺失。
To get this you have many ways. 为了得到这个,你有很多方法。
Method 1: 方法1:
Removing require should do the trick. 删除require应该可以解决问题。
params.permit(:type)
Method2: 方法2:
Add a user[]
as a name to the input in the edit form. 将
user[]
作为名称添加到编辑表单中的输入。
Will elaborate the method 1 if you add the edit form. 如果添加编辑表单,将详细说明方法1。
Method3: 方法3:
The other solution is to add the user key to the parameters. 另一种解决方案是将用户密钥添加到参数中。
def update
if current_admin
params.user = {
"type": params.type,
"id": params.id
}
if @user.update(user_params)
redirect_to users_path
else
render 'edit'
end
end
end
private
def user_params
params.require(:user).permit(:type)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.