Abdul Ahmad Abdul Ahmad - 4 months ago 12
Ajax Question

rails adding custom route to existing resource

in my routes.rb file I have a resource:

resources :authentication

but I also want to create a custom route, so I have the following UNDER the previous line:

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

and I ran
bin/rake routes

and my controller has this:

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

however, when I try to access this route I keep getting a 404. This is how I'm trying to access:

method: "GET",
url: "/authentication/is_signed_in.json"

am I missing something? do I have to do something special to allow a route with a


You don't need to use scope here. Just add the following before the line with resources :authentication:

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

Alternatively, and perhaps more canonically (see the docs), you can add more action to a given resource like this:

resources :authentication do
  get 'is_signed_in'

However, in this case you might need to change the name of the is_signed_in? method in your AuthenticationController to is_signed_in (without the ? at the end).