nil的未定义方法`update':NilClass rails 4.2

时间:2014-12-27 09:47:05

标签: ruby-on-rails ruby-on-rails-4

我知道还有其他类似的问题,但它们对我不起作用。这个one非常相似,但我不知道答案代码与问题有什么不同。

这是我的错误消息

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

这是我的控制器



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
&#13;
&#13;
&#13;

服务器日志错误

&#13;
&#13;
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'
&#13;
&#13;
&#13;

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您必须允许id param用于您的食谱。由于您未允许,以下内容会返回nil

@recipe = Recipe.find(params[:id])

因此,请将recipe_params方法更改为:

params.require(:recipe).permit(:id, :name, :author, :body, :picture)

更新

我看到您已更新代码,但未在任何before_action中设置@recipe变量。如果是这种情况,那么您需要检查哪个参数用于配方的ID,然后在实际更新配方之前在任何地方执行以下操作:

@recipe = Recipe.find(whatever_is_the_param_for_recipe_id)

如果这是用户控制器,则不应该@recipe.update(user_params)。你很可能意味着@user.update(user_params)