Ruby Devise、SessionsController.create、json - 得到 NameError: undefined 'build_resource'? [英] Ruby Devise, SessionsController.create, json - getting NameError: undefined 'build_resource'?

查看:15
本文介绍了Ruby Devise、SessionsController.create、json - 得到 NameError: undefined 'build_resource'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Ruby 生态系统还很陌生,快要被淹没了.我想我被 Visual Studio w C# 的简单智能所宠坏了.无论如何,我在 Ubuntu 上使用 Ruby 1.9.3、Rails 3.2.13 和 Devise 3.0.3.我可以通过 PC 上的浏览​​器登录到该网站.但是,当我尝试从我们的 Phonegap 移动应用中执行此操作时,出现此错误:

I'm rather new to the Ruby ecosystem and am drowning. I guess I was spoiled by the easy intellisense of Visual Studio w C#. Anyway, I'm using Ruby 1.9.3, Rails 3.2.13, and Devise 3.0.3 on Ubuntu. I can login to this website, via a browser on a PC. But when I attempt to do it from our Phonegap mobile-app, I get this error:

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

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

这是sessions_controller.rb中的代码:

Here is the code within 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 的那一行.我很感激任何帮助指出我去哪里.那条线到底有什么作用?那是方法调用吗?如何发现那叫什么?

Evidently, it is the line that contains build_resource, that is producing the error. I'd appreciate any help to point me where to go. What does that line even do? Is that a method-call? How does one discover what that calls?

推荐答案

如果你去 此处 您会看到设计 registrations_controller.

If you go here you see the devise registrations_controller.

它有 build_resource 方法,你正在调用你的 session_controller

It has the build_resource method, that you are calling in you 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

问题在于它是受保护的(在表示 protected 的行下)这意味着 build_resource 方法只能从设计 registrations_controller 调用.

The problem is that it is protected ( under the line that says protected ) That means that the build_resource method can only be called from the devise registrations_controller.

它与浏览器一起工作的原因是 session_controller 中的 create 操作调用

The reason it works with the browser it that the create action in you sessions_controller calls

super

这意味着它从设计 session_controller 调用创建操作,你的 session_controller 继承自 -

This means that it calls the create action from the devise sessions_controller, which your sessions_controller inherits from -

#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 登录用户.

This gist shows how to login users trough a json api.

它使用这个包含

include Devise::Controllers::InternalHelpers

在 session_controller 中.我认为这使得使用 build_resource 方法成为可能.

in the sessions_controller. I think this makes it possible to use the build_resource method.

祝你好运!

编辑

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

这篇关于Ruby Devise、SessionsController.create、json - 得到 NameError: undefined 'build_resource'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆