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

相关问题