未定义方法“保存”对于 nil:NilClass

发布于 2024-10-12 23:05:48 字数 1860 浏览 2 评论 0原文

我在尝试注册新用户时收到此错误。

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):
app/controllers/users_controller.rb:49:in
create'
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春庭雪 2024-10-19 23:05:48

没有看到你的视图代码,很难说,但是由于你已经注释掉了发送到新视图的 @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?

撩人痒 2024-10-19 23:05:48

发生错误是因为 @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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文