【发布时间】:2014-12-27 09:47:04
【问题描述】:
我知道还有其他与此类似的问题,但它们对我不起作用。这个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
服务器日志错误
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'
感谢您的帮助!
【问题讨论】:
-
更新代码以反映 Humza 的第一点也意识到我复制了错误的控制器。
-
配方参考应替换为用户
标签: ruby-on-rails ruby-on-rails-4