2016-06-14 39 views
1

有資源:軌添加自定義路線在我的routes.rb我的文件現有資源

resources :authentication 

,但我也想創建一個自定義路由,所以我的前行下以下幾點:

scope :authentication do 
    get 'is_signed_in', to: 'authentication#is_signed_in?' 
end 

,我跑bin/rake routes

和我的控制器有這樣的:

class AuthenticationController < ApplicationController 
    def is_signed_in? 
    if user_signed_in? 
     render :json => {"signed_in" => true, "user" => current_user}.to_json() 
    else 
     render :json => {"signed_in" => false}.to_json() 
    end 
    end 
end 

然而,當我嘗試訪問這條路線我不斷收到一個404這是我正在嘗試訪問:

$.ajax({ 
    method: "GET", 
    url: "/authentication/is_signed_in.json" 
}) 

我這麼想嗎?我是否必須做一些特殊的事情來允許延長.json的路線?

回答

1

這裏您不需要使用scope。只是resources :authentication添加以下之前行:

get 'authentication/is_signed_in', to: 'authentication#is_signed_in?' 

或者,也許更規範地(see the docs),你可以這樣對給定資源添加更多的行動:

resources :authentication do 
    get 'is_signed_in', on: :collection 
end 

然而,在這種情況下,您可能需要將AuthenticationControlleris_signed_in?方法的名稱更改爲is_signed_in(末尾沒有?)。

+0

所以第二個選項創建'資源',然後用'do'我可以分配額外的可選路由? –

+0

基本上,是的。但是現在我看到'resources'塊中的'get'方法是不完整的。它還需要'on::collection'才能正常工作。我再次編輯了我的答案。 –

相關問題