Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Building Social Features in Ruby on Rails Building the Friendship UI Creating the Friendship

Rails 4 Friendship Routes

So the 'Creating the Friendship' section is working on renaming devise routes for our purposes. Specifically changing sign_in to login. Here is their suggested code.

as :user do
    get "/register", to: "devise/registrations#new", as: :register
    get "/login", to: "devise/sessions#new", as: :login
    get "/logout", to: "devise/sessions#destroy", as: :logout
end

devise_for :users, skip: [:sessions]

as :user do
    get "/login" => 'devise/sessions#new', as: :new_user_session
    post "/login" => 'devise/sessions#create', as: :user_session
    delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
end

In Rails 4 this doesn't work. Per this Devise issue, "Rails 4 router doesn't support identically named routes with conditional logic"

Does anyone have a workaround for this?

1 Answer

Actually I'm just an idiot. Devise creates a resource when you first install devise. This lesson has you change it but in Rails 4 you can't have matching resource names. So the code actually looks like this (in Rails 3.2.6 aka the video)

devise_for :users

as :user do
    get "/register", to: "devise/registrations#new", as: :register
    get "/login", to: "devise/sessions#new", as: :login
    get "/logout", to: "devise/sessions#destroy", as: :logout
end

devise_for :users, skip: [:sessions]

as :user do
    get "/login" => 'devise/sessions#new', as: :new_user_session
    post "/login" => 'devise/sessions#create', as: :user_session
    delete "/logout" => 'devise/sessions#destroy', as: :destroy_user_session
end

The solution for Rails 4 is to delete the first devise_for :users