gpt4 book ai didi

ruby-on-rails - rails : parameter missing in update action

转载 作者:行者123 更新时间:2023-12-04 03:38:41 27 4
gpt4 key购买 nike

这是错误消息:

ActionController::ParameterMissing in UsersController#update
param is missing or the value is empty: user

完整的错误日志
ActionController::ParameterMissing (param is missing or the value is empty: user):
app/controllers/users_controller.rb:36:in `update_user_params'
app/controllers/users_controller.rb:15:in `update'

错误突出显示了这一行:
def update_user_params
params.require(:user).permit(:role)
end

错误的参数是:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"NVSo53F3PFIhOxDthMFCAxgTIDAewnxppsGTNzkp/Zc=",
"admin"=>{"role"=>"user"},
"commit"=>"Make User",
"id"=>"2"}

这是我正在使用的表格:show.html.erb
<tr id="dashfield">
<td><%= @user.first_name%></td>
<td><%= @user.last_name %></td>
<td><%= @role %></td>
<td><% if @role == 'admin'%>
<%= form_for @role, method: :put do |f|%>
<%= f.hidden_field :role, :value => 'user'%>
<%= f.submit 'Make User'%><%end%>
<%else%>
<%= form_for @role, method: :put do |f| %>
<%= f.hidden_field :role, :value => 'admin'%>
<%= f.submit 'Make Admin'%>
<%end%>
<%end%></td>
</tr>

这是 Controller :
class UsersController < ApplicationController
def new
@user = User.new
end
def index
@allusers = User.all
end
def show
@user = User.find(params[:id])
@role = @user.role
end
def update
@user = User.find(params[:id])
if @user.update(update_user_params)
redirect_to '/users'
else
redirect_to "/users/#{id}"
end
end
def create
@user = User.new(user_params)
@user.role = 'user'
if @user.save
session[:user_id] = @user.id
redirect_to '/hours/new'
else
redirect_to '/signup'
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :email, :password)
end
def update_user_params
params.require(:user).permit(:role)
end
end

错误是说它找不到数据库,用户吗?这个问题有什么解决方案吗?

最佳答案

你的错误是说:

ActionController::ParameterMissing in UsersController#update param is missing or the value is empty: user

因为您的 update_user_params需要 user出现在参数中。由于它目前不存在于您的参数中,因此它会引发此错误。

here知道如何 params.require作品。

在您的表单中,您可以尝试添加如下内容:
<%= f.hidden_field :user, { :value => @user } %>

关于ruby-on-rails - rails : parameter missing in update action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32038841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com