2014-03-31 22 views
1

我对Ruby生态系统相当陌生,并且溺水。我想我被Visual Studio w C#的easy intellisense所宠坏了。无论如何,我在Ubuntu上使用Ruby 1.9.3,Rails 3.2.13和Devise 3.0.3。 我可以通过PC上的浏览器登录本网站。但是,当我试图从我们的PhoneGap移动应用做到这一点,我得到这个错误:Ruby Devise,SessionsController.create,json - 获取NameError:undefined'build_resource'?

NameError: 'undefined local variable or method 'build_resource' for # ..

这里是sessions_controller.rb中的代码:

class SessionsController < Devise::SessionsController 
    def create 
    respond_to do |format| 
     format.html { 
     super 
     } 
     format.json { 
     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 
.. 

显然,这是一个包含行build_resource,这是产生错误。我很感激任何帮助,指出我要去哪里。这条线甚至可以做什么?这是一种方法调用吗?人们如何发现那些呼吁?

回答

4

如果你去here你会看到设计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方法只能从被称为设计registrations_controller。

它与浏览器的原因,它在你创建行动sessions_controller调用

super 

这意味着,它调用从色器件sessions_controller,你的sessions_controller继承的创建行动 -

#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 

这个gist显示了如何通过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 
+0

这个项目上我的工作,* *是以前的工作。我没有改变Devise代码中的任何东西。为什么build_resource突然变得不确定? – JamesWHurst

+0

你做了什么改变?更新设计?删除了一些代码?当你得到未定义的错误时,你正在运行的代码看不到该方法。您的会话控制器中是否包含“包含Device:Controllers :: IneternalHelpers”行。从技术上讲,您可以使用会话控制器更改设计代码。只要你不叫super,它会覆盖'Devise :: SessionsController' –

+0

你去哪儿了? –