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
的路線?
所以第二個選項創建'資源',然後用'do'我可以分配額外的可選路由? –
基本上,是的。但是現在我看到'resources'塊中的'get'方法是不完整的。它還需要'on::collection'才能正常工作。我再次編輯了我的答案。 –