Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails NoMethodError-未定义的方法`用户';零级:零级_Ruby On Rails_Ruby_Omniauth - Fatal编程技术网

Ruby on rails NoMethodError-未定义的方法`用户';零级:零级

Ruby on rails NoMethodError-未定义的方法`用户';零级:零级,ruby-on-rails,ruby,omniauth,Ruby On Rails,Ruby,Omniauth,我正在使用Omniauth尝试注册/登录我的web应用程序上的用户。 这发生在我的AuthenticationsController#Create方法中: 身份验证控制器: class AuthenticationsController < InheritedResources::Base def create omniauth = request.env['omniauth.auth'] authentication = Authentication.find_by

我正在使用Omniauth尝试注册/登录我的web应用程序上的用户。 这发生在我的
AuthenticationsController#Create
方法中:

身份验证控制器:

class AuthenticationsController < InheritedResources::Base

  def create
    omniauth = request.env['omniauth.auth']
    authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
    if authentication
      flash[:success] = "Signed in successfully"
      sign_in_and_redirect(authentication.user)
    elsif current_user
      token = omniauth['credentials'].token
      secret = omniauth['credentials'].secret
      current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'], :token => token, :secret => token_secret)
      flash[:success] = "Authentication successful"
      redirect_to authentications_url
    else
      user = User.new
      user.apply_omniauth(omniauth)
      if user.save!
        flash[:success] = "Account created"
        sign_in(authentication.user)
      else
        session[:omniauth] = omniauth.except('extra')
        redirect_to '/signup'
      end
    end
  end
类身份验证控制器omniauth['provider'],:uid=>omniauth['uid'],:token=>token,:secret=>token\u secret)
flash[:success]=“身份验证成功”
将\u重定向到身份验证\u url
其他的
user=user.new
user.apply\u omniauth(omniauth)
如果user.save!
flash[:success]=“已创建帐户”
登录(authentication.user)
其他的
会话[:omniauth]=omniauth.except('extra')
将_重定向到“/signup”
结束
结束
结束

我最初有
登录(:user,authentication.user)
但它给了我参数错误,所以我在上面的代码中将它改为
有登录(authentication.user)
。然而,现在我为nil:NilClass获得了一个
NoMethodError-未定义的方法'user'

首先测试是否定义了
authentication
,然后在
else
块中(该块仅在未定义时才会触发),出于某种原因,您去引用
authentication.user
。这不可能奏效

如果您未进行身份验证且未登录,则该条件似乎适用。在这种情况下,您为什么要使用
登录
功能?

第23行
登录(authentication.user)
失败,因为您处于
的else分支,如果身份验证
条件(因此
身份验证
为零)。
您可能想做的是调用
登录(用户)
,为您刚刚在上面创建了几行的用户登录。

如果您在数据库中使用一组提供程序,请确保

> :provider => omniauth['provider']

例如,与数据库中的提供程序匹配。如果数据库中有
facebook\u Oauth2
,并且提供商的响应是
facebook
身份验证将失败

我将其更改为
登录并重定向(用户)
,但我仍然得到未定义的局部变量或方法“user”,该控制器的正确“def create”应该是什么样子?我试着跟着火车去omniauth