[英]undefined method `update' for nil:NilClass rails 4.2
I know there are other similar questions to this one but they don't work for me. 我知道还有其他与此类似的问题,但它们对我不起作用。 This one is very similar but I don't see how the answer code is any different from the question.
这一个是非常相似,但我没有看到答案代码是如何从这个问题有什么不同。
Here is my error message 这是我的错误信息
NoMethodError in UsersController#update
undefined method `update' for nil:NilClass
def update
respond_to do |format|
if @recipe.update(user_params)
format.html { redirect_to @user, notice: 'User was successfully updated.' }
format.json { render :show, status: :ok, location: @user }
else
Here is my controller 这是我的控制器
class UsersController < ApplicationController before_action :set_user, only: [:edit, :update] def new @user = User.new end def create @user = User.new(user_params) if @user.save redirect_to root_url, :notice => "Signed up!" else render "new" end end def edit end # PATCH/PUT /users/1 # PATCH/PUT /users/1.json def update respond_to do |format| if @recipe.update(user_params) format.html { redirect_to @user, notice: 'User was successfully updated.' } format.json { render :show, status: :ok, location: @user } else format.html { render :edit } format.json { render json: @user.errors, status: :unprocessable_entity } end end end private # Use callbacks to share common setup or constraints between actions. def set_user @user = User.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. def user_params params.require(:user).permit(:id, :name, :email, :password, :password_confirmation, :avatar) end end
server log errors 服务器日志错误
Started GET "/users/1/edit" for 127.0.0.1 at 2014-12-27 11:42:26 -0600 Processing by UsersController#edit as HTML Parameters: {"id"=>"1"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Rendered users/_form.html.erb (2.2ms) Rendered users/edit.html.erb within layouts/application (3.4ms) User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Completed 200 OK in 247ms (Views: 245.9ms | ActiveRecord: 0.6ms) Started PATCH "/users/1" for 127.0.0.1 at 2014-12-27 11:42:39 -0600 Processing by UsersController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"NIo0M4TntTzi4TDTsu+dWsSVHfnSnYFbW0Beew1YrApuebRNY9aadcI3/TzSsVN1Lc3R0iWbPGcxfjdW1Ovg4A==", "user"=>{"name"=>"Nathan Davis", "email"=>"NathanTheGreat94@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "avatar"=>#<ActionDispatch::Http::UploadedFile:0x007f819c31d790 @tempfile=#<Tempfile:/var/folders/09/vs8567nx49v9mzhv7r6dyxrh0000gn/T/RackMultipart20141227-50092-j9p25d.jpg>, @original_filename="formal.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\\"user[avatar]\\"; filename=\\"formal.jpg\\"\\r\\nContent-Type: image/jpeg\\r\\n">}, "commit"=>"Update User", "id"=>"1"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] Completed 500 Internal Server Error in 2ms NoMethodError (undefined method `update' for nil:NilClass): app/controllers/users_controller.rb:25:in `block in update' app/controllers/users_controller.rb:24:in `update'
thanks for any help! 谢谢你的帮助!
You have to allow the id
param for your recipe. 您必须允许您的食谱使用
id
参数。 Because you haven't allowed it, the following returns nil
由于您不允许这样做,因此以下命令返回
nil
@recipe = Recipe.find(params[:id])
So change your recipe_params
method to this: 因此,将您的
recipe_params
方法更改为此:
params.require(:recipe).permit(:id, :name, :author, :body, :picture)
Update: 更新:
I see that you've updated your code but that the @recipe
variable isn't being set in any before_action. 我看到您已经更新了代码,但是没有在任何before_action中设置
@recipe
变量。 If this is the case, then you need to check which param is for the recipe's ID and then do the following anywhere before you actually update the recipe: 如果是这种情况,则需要检查配方ID的参数,然后在实际更新配方之前在任何地方进行以下操作:
@recipe = Recipe.find(whatever_is_the_param_for_recipe_id)
And if this is the user controller, you shouldn't be doing @recipe.update(user_params)
. 如果这是用户控制器,则不应执行
@recipe.update(user_params)
。 You most likely meant @user.update(user_params)
您最有可能的意思是
@user.update(user_params)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.