2016-04-26 69 views
1

我测试用色器件AJAX和我遇到了此问题在控制台:设计阿贾克斯404路由错误

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

和Rails服务器返回:

ActionController::RoutingError (uninitialized constant UsersController): 

的代码一点点:

配置/ 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' 

初始化/ 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 

在视图/设计/注册有new.html.erb,get_company.js.coffee和资产/ Javascript角有是get_company.js.coffee。 这些似乎工作正常,因为事件发生,然后我得到路由错误。

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

回答

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

+0

谢谢!出于某种原因,位置2没有工作,它回来后发出一条消息,告诉我用范围包装它。所以位置1是正常工作的...以及正确的语法。 – six7zero9