ActiveModel::ForbiddenAttributesErrorRor4
ActiveModel::ForbiddenAttributesError Ror4
正在将较旧的 rails 应用升级到 4.2.10。我正在使用魔法Gem。该应用程序在 RoR 3.2.8
中运行良好
第 45 行 ---> @user = User.new(params[:user])
提取的源代码(大约第 45 行):
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
定义创建方法
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { render action: "thanks", notice: 'User was
successfully created.' }
format.json { render json: @user, status: :created,
location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end
在 rails 4 或更高版本中,您需要使用以下强参数
def permit_user_params
params.require(:user).permit(:name, :email, :etc)
end
并将此允许的参数用作
def create
@user = User.new(permit_user_params)
respond_to do |format|
if @user.save
format.html { render action: "thanks", notice: 'User was
successfully created.' }
format.json { render json: @user, status: :created,
location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end
希望对您有所帮助。
正在将较旧的 rails 应用升级到 4.2.10。我正在使用魔法Gem。该应用程序在 RoR 3.2.8
中运行良好第 45 行 ---> @user = User.new(params[:user])
提取的源代码(大约第 45 行):
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
# PUT /users/1
# PUT /users/1.json
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
定义创建方法
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { render action: "thanks", notice: 'User was
successfully created.' }
format.json { render json: @user, status: :created,
location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end
在 rails 4 或更高版本中,您需要使用以下强参数
def permit_user_params
params.require(:user).permit(:name, :email, :etc)
end
并将此允许的参数用作
def create
@user = User.new(permit_user_params)
respond_to do |format|
if @user.save
format.html { render action: "thanks", notice: 'User was
successfully created.' }
format.json { render json: @user, status: :created,
location: @user }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status:
:unprocessable_entity }
end
end
end
希望对您有所帮助。