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'才能正常工作。我再次编辑了我的答案。 –