【问题标题】:Devise Ajax 404 routing error设计 Ajax 404 路由错误
【发布时间】:2016-04-26 20:18:18
【问题描述】:

我正在使用 devise 测试 ajax 并在控制台中遇到此错误:

GET https://example.com/users/get_company?foo=bar 404 (Not Found)

Rails 服务器正在返回:

ActionController::RoutingError (uninitialized constant UsersController):

一点代码:

config/routes.rb

devise_for :users, :controllers => {registrations: 'registrations'}
devise_scope :user do
  authenticated :user do
    root 'account#show', as: :authenticated_root
  end

  unauthenticated do
    root 'devise/sessions#new', as: :unauthenticated_root
  end
end    

get 'users/get_company', as: 'get_company'

initializers/devise.rb

config.http_authenticatable_on_xhr = true
config.navigational_formats = ['*/*', :html]

控制器/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
  respond_to :json
  def get_company
    @company = Company.first.name
    respond_to do |format|
      format.js
    end
  end
end

在views/devise/registrations 中有new.html.erb、get_company.js.coffee,在assets/javascripts 中有get_company.js.coffee。 这些似乎工作正常,因为事件发生了,然后我收到了路由错误。

任何帮助将不胜感激,因为我只是在学习这个以帮助完成一个项目。请询问任何其他代码或任何可能有助于更好地解释这种情况的代码。

【问题讨论】:

    标签: ruby-on-rails ruby devise


    【解决方案1】:

    嘿,您的get_company 路线丢失了/,所以应该是:

    devise_for :users, :controllers => {registrations: 'registrations'}
    devise_scope :user do
      authenticated :user do
        root 'account#show', as: :authenticated_root
      end
    
      unauthenticated do
        root 'devise/sessions#new', as: :unauthenticated_root
      end
      # 1: If you put your get_company route here, it still works
      get '/users/get_company' => 'registrations#get_company', as: 'get_company'
    end    
    
    # 2: If you put your get_company route here, it works as well
    # get '/users/get_company' => 'registrations#get_company', as: 'get_company'
    

    无论您将路线放在#1#2 的位置,您的代码都可以正常工作

    【讨论】:

    • 谢谢!出于某种原因,位置 2 不起作用,它返回一条消息,告诉我用范围包装它。所以位置 1 是有效的......当然还有正确的语法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-05
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多