Ruby Devise,SessionsController.create,json – 获取NameError:undefined’build_resource’?

我对Ruby生态系统很陌生并且淹死了。 我想我被Visual Studio w C#的简单intellisense破坏了。 无论如何,我在Ubuntu上使用Ruby 1.9.3,Rails 3.2.13和Devise 3.0.3。 我可以通过PC上的浏览器登录这个网站。 但当我尝试从我们的Phonegap移动应用程序中执行此操作时,我收到此错误:

NameError:’未定义的局部变量或方法’build_resource’用于#..

以下是sessions_controller.rb中的代码:

class SessionsController < Devise::SessionsController def create respond_to do |format| format.html { super } format.json { build_resource #  params[:user][:email]) return invalid_login_attempt unless resource return invalid_login_attempt unless user .. 

显然,它是包含build_resource的行,即产生错误。 我很感激任何指示我去哪里的帮助。 这条线甚至做了什么? 这是方法调用吗? 如何发现所谓的内容?

如果你去这里,你会看到devise registrations_controller。

它有build_resource方法,您在sessions_controller中调用它

  # Build a devise resource passing in the session. Useful to move # temporary session data to the newly created user. def build_resource(hash=nil) self.resource = resource_class.new_with_session(hash || {}, session) end 

问题是它受到保护(在表示受保护的行下)这意味着build_resource方法只能从devise registrations_controller中调用。

它与浏览器一起工作的原因是你在sessions_controller调用中创建动作

 super 

这意味着它从设备sessions_controllerinheritance的设计sessions_controller调用create动作 –

 #devise/sessions_controller def create self.resource = warden.authenticate!(auth_options) set_flash_message(:notice, :signed_in) if is_flashing_format? sign_in(resource_name, resource) yield resource if block_given? respond_with resource, location: after_sign_in_path_for(resource) end 

这个要点显示了如何通过json api登录用户。

它使用这个包括

 include Devise::Controllers::InternalHelpers 

在sessions_controller中。 我认为这可以使用build_resource方法。

祝好运!

编辑

 def create respond_to do |format| # when you log into the application through a web browser you go to the format.html option # Thus you're not calling the build_resource method format.html { super } # So, lets try to sign in without the build_resource # I am not really sure what you can do, but try this format.json { resource = User.find_for_database_authentication(:login=>params[:user_login][:login]) return invalid_login_attempt unless resource if resource.valid_password?(params[:user_login][:password]) sign_in("user", resource) render :json=> {:success=>true, :auth_token=>resource.authentication_token, :login=>resource.login, :email=>resource.email} return end invalid_login_attempt end # build_resource # <-This line is evidently producing an error! # user = User.find_for_database_authentication(:email => params[:user][:email]) # return invalid_login_attempt unless resource # return invalid_login_attempt unless user