我已经开始为我的rails应用程序创建一个Api。我目前创建会话控制器日志。会话Api,undefined方法downcase
但由于某些原因,我得到这个错误
NoMethodError
in Api::V1::SessionsController#create
undefined method `downcase' for nil:NilClass;
我不理解为什么会这样。我也在我的传统会话控制器中使用了downcase方法,而我没有这个问题。
API控制器
module Api
module V1
class SessionsController < ApplicationController
skip_before_filter :verify_authenticity_token,
:if => Proc.new { |c| c.request.format == 'application/json' }
respond_to :json
def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
end
render :status => 200,
:json => { :success => true,
:info => "Logged In Successfully",
:data => { } }
end
end
end
end
控制器
class SessionsController < ApplicationController
def create
user = User.find_by_email(params[:session][:email].downcase) ###THIS WORKS FINE
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to publishers_path
end
end
end
ROUTES
# API Routes
namespace :api, defaults: {format: 'json'} do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :sessions, only: [:new, :create, :destroy]
end
end
检查你是否有任何时髦的白色空格后。 – BroiSatse 2014-08-29 22:24:51
是的,我不,它的确很奇怪...... – 2014-08-29 22:28:17
哦,你的编辑很需要。 'params [:session] [:email]'是零,所以这就是为什么你的失败失败。由于它是一个API,您应该确实在检查以确保人们向您传递成功拨打电话所需的所有有效数据。 – JTG 2014-08-29 22:29:53