简体   繁体   English

Api :: SessionsController#create中的NoMethodError

[英]NoMethodError in Api::SessionsController#create

I keep getting this error in my sessions controller in my rails 5 api: 我在Rails 5 API中的会话控制器中不断收到此错误:

undefined method `[]' for nil:NilClass

I'm honestly not sure what i'm doing wrong. 老实说,我不确定自己在做什么错。 Here is my code for my sessions controller (error occurs on line 8): 这是我的会话控制器的代码(错误发生在第8行):

module Api
class SessionsController < ApplicationController

def new
end

def create
  # The error occurs on this line
  @user = @User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      render :text => user.access_token, status: 200
    else
      render text: "Email and password combination are invalid", status: 422
    end
end


def verify_access_token
  user = User.find_by(access_token: params[:session][:access_token])
    if user
      render text: "verified", status: 200
    else
      render text: "Token failed verification", status: 422
    end
  end

 end
end

here is my sesssions helper: 这是我的助手助手:

module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end

# Remembers a user in a persistent session.
# signed creates an encrypted cookie so that's why we use it.
def remember(user)
  user.remember
  cookies.permanent.signed[:user_id] = user.id
  cookies.permanent[:remember_token] = user.remember_token
end

# Returns true if the given user is the current user.
def current_user?(user)
  user == current_user
end

# Returns the current logged-in user (if any).
def current_user
  if (user_id = session[:user_id])
   @current_user ||= User.find_by(id: user_id)
  elsif (user_id = cookies.signed[:user_id])
    user = User.find_by(id: user_id)
    if user && user.authenticated?(cookies[:remember_token])
    log_in user
    @current_user = user
   end
 end
end

# Returns true if the user is logged in, false otherwise.
def logged_in?
 !current_user.nil?
end

# Forgets a persistent session.
def forget(user)
  user.forget
  cookies.delete(:user_id)
  cookies.delete(:remember_token)
end

def log_out
  forget(current_user)
  session.delete(:user_id)
  @current_user = nil
end

# Redirects to stored location (or to the default).
def redirect_back_or(default)
  redirect_to(session[:forwarding_url] || default)
  session.delete(:forwarding_url)
end

# Stores the URL trying to be accessed.
def store_location
  session[:forwarding_url] = request.url if request.get?
end

If anyone can help help me find a solution to this error, it would be deeply appreciated. 如果有人可以帮助我找到解决此错误的方法,将不胜感激。 I'm pretty new when it comes to JSON api's. 在JSON API方面,我还很新。

I'm betting params[:session] is nil. 我打赌params [:session]为零。 This is why chaining hash keys is a bad idea. 这就是为什么链接哈希键是一个坏主意的原因。

See also Hash#dig https://ruby-doc.org/core-2.3.0/Hash.html#method-i-dig 另请参见Hash#dig https://ruby-doc.org/core-2.3.0/Hash.html#method-i-dig

(which is news to me!) (对我来说是新闻!)

@User is not a valid way to look up ActiveRecord objects. @User不是查找ActiveRecord对象的有效方法。 It should be the name of the model, capitalized, then the find method. 它应该是模型的名称,大写,然后是find方法。

Change @user = @user... to to @user = User.find_by... and you should be able to complete the lookup properly. @user = @user...更改为@user = User.find_by... ,您应该能够正确完成查找。

One thing I like to do is drop a byebug above the line in question and then build up the query 'dot-by-dot' in the terminal to see if I can find where I've gone wrong. 我想做的一件事是在问题所在的行上方放置一个byebug,然后在终端中建立查询“ dot-by-dot”,以查看是否可以找到我出错的地方。 It will also let you look inside the params to make sure there is something there. 它还可以让您查看参数内部,以确保其中存在某些内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SessionsController#create中的NoMethodError - NoMethodError in SessionsController#create SessionsController#create中的NoMethodError - NoMethodError in SessionsController#create rails 4:Devise :: SessionsController#create中的NoMethodError - rails 4 : NoMethodError in Devise::SessionsController#create SessionsController#NoilMethodError为nil:NilClass创建未定义的方法“ []” - NoMethodError in SessionsController#create undefined method `[]' for nil:NilClass SessionsController#中的Rails 4 NoMethodError为nil:NilClass创建未定义的方法&#39;slice&#39; - Rails 4 NoMethodError in SessionsController#create undefined method `slice' for nil:NilClass SessionsController#create中的NoMethodError-#的未定义方法&#39;[]&#39; <Oauth::Access_Token…> - NoMethodError in SessionsController#create - Undefined method '[]' for #<Oauth::Access_Token…> 设计中的 NoMethodError::SessionsController#create undefined method `current_sign_in_at&#39; - NoMethodError in Devise::SessionsController#create undefined method `current_sign_in_at' Rails Devise LDAP错误:Devise :: SessionsController#create中的NoMethodError - Rails Devise LDAP Error: NoMethodError in Devise::SessionsController#create 在用户模型中调用方法时,SessionsController#create中的NoMethodError - NoMethodError in SessionsController#create when calling a method in user model 使用Omniauth Facebook身份验证Railscast 360在SessionsController#create中出现NoMethodError - NoMethodError in SessionsController#create using Omniauth Facebook authentication railscast 360
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM