未定义方法“保存”对于 nil:NilClass
我在尝试注册新用户时收到此错误。
NoMethodError(未定义方法 save' for nil:NilClass): app/controllers/users_controller.rb:49:in
创建' 应用程序/控制器/users_controller.rb:48:在“创建”中
class UsersController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
#filter_resource_access
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
def new
#@user = User.new
@carriers = Carrier.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
def edit
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def profile
@user = User.find(params[:id])
end
def update
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
def delete
@user = User.find(params[:user_id])
@user.destroy
redirect_to :users
end
end
I get this error when trying to register a new user.
NoMethodError (undefined method save' for nil:NilClass):
create'
app/controllers/users_controller.rb:49:in
app/controllers/users_controller.rb:48:in `create'
class UsersController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
#filter_resource_access
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
def show
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @user }
end
end
def new
#@user = User.new
@carriers = Carrier.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @user }
end
end
def edit
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "Account registered!"
redirect_back_or_default account_url
else
render :action => :new
end
end
def profile
@user = User.find(params[:id])
end
def update
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@user = User.find(params[:id])
@user.destroy
respond_to do |format|
format.html { redirect_to(users_url) }
format.xml { head :ok }
end
end
def delete
@user = User.find(params[:user_id])
@user.destroy
redirect_to :users
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有看到你的视图代码,很难说,但是由于你已经注释掉了发送到新视图的
@user
对象,也许从视图返回的参数没有定义为键值表单中的对作为用户属性?without seeing your view code, its hard to say, but since you have commented out the
@user
object that is sent to the new view, perhaps the params coming back from the view are not defined as key value pairs from the form as user attributes?发生错误是因为 @user 变量为零,因此调用 (nil).save 将失败。这意味着 User.new(params[:user]) 返回 nil,这是永远不应该发生的。 User.new 应该始终返回一条记录,即使它无效。
因此,由于控制器看起来很普通,我会在黑暗中进行尝试并建议检查您的用户模型是否有“新”方法。您可能会使用一些返回 nil 并破坏控制器的逻辑来覆盖 ActiveRecord 的“新”方法。如果是这样,您必须将“def new”替换为不同的方法名称。
如果这没有帮助,请向我们提供您的用户模型中的代码。
The error is happening because the @user variable is nil, so calling (nil).save will fail. This means that User.new(params[:user]) is returning nil, which should never happen. User.new should always return a record, even if it is invalid.
So, since the controller looks pretty ordinary, I'll take a shot in the dark and suggest checking your User model for a 'new' method. You might be overriding ActiveRecord's 'new' method with some logic that returns nil and breaks your controller. If so, you must replace 'def new' with a different method name.
If that doesn't help, please provide us with the code in your User model.