2016-04-26 17 views
1

私は工夫してAJAXをテストしていて、コンソールでこのエラーに実行しています:工夫アヤックス404ルーティングエラー

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

とサーバが返しているレール:

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' 

の初期化子/ 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