我有 "ActiveModel::ForbiddenAttributesError in UsersController#create"
am having "ActiveModel::ForbiddenAttributesError in UsersController#create"
请尝试 运行 这段代码,但我在 UsersController#create 中确实有 ActiveModel::ForbiddenAttributesError 错误,请问我该如何修复它
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
#debuger
end
def agent_signup
@user = User.new
end
def create
@user = User.new(params[:user]) # Not the final implementation!
if @user.save
# Handle a successful save.
else
render 'agent_signup'
end
end
private
def user_params
params.require(:user).permit(:name, :email, #:password,
#:password_confirmation
)
end
end
应该是
# Wrong
@user = User.new(params[:user]) # Not the final implementation!
# Correct
@user = User.new(user_params) # Not the final implementation!
为了使用允许的参数
试试这个 solution.You 需要允许参数。
def create
@user = User.new(user_params)
if @user.save
# Handle a successful save.
else
render 'agent_signup'
end
end
请尝试 运行 这段代码,但我在 UsersController#create 中确实有 ActiveModel::ForbiddenAttributesError 错误,请问我该如何修复它
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
#debuger
end
def agent_signup
@user = User.new
end
def create
@user = User.new(params[:user]) # Not the final implementation!
if @user.save
# Handle a successful save.
else
render 'agent_signup'
end
end
private
def user_params
params.require(:user).permit(:name, :email, #:password,
#:password_confirmation
)
end
end
应该是
# Wrong
@user = User.new(params[:user]) # Not the final implementation!
# Correct
@user = User.new(user_params) # Not the final implementation!
为了使用允许的参数
试试这个 solution.You 需要允许参数。
def create
@user = User.new(user_params)
if @user.save
# Handle a successful save.
else
render 'agent_signup'
end
end