'Rails Routes: as-block and device_for block

According to devise documentation, we can override the default session paths using the following:

as :user do
   get 'login', to: 'users/sessions#new', as: :new_user_session
   post 'login', to: 'users/sessions#create', as: :user_session
   delete 'logout', to: 'users/sessions#destroy', as: :destroy_user_session
end 

This will generate the following routes:

Helper Path                   URL        Controller
new_user_session_path        /login      users/sessions#new
user_session_path            /login      users/sessions#create
destroy_user_session_path    /logout     users/sessions#destroy

However, without wrapping the defined path inside an as-block or a devise_scope block, such as the one below, we produce the same exact routes.

get 'login', to: 'users/sessions#new', as: :new_user_session
post 'login', to: 'users/sessions#create', as: :user_session
delete 'logout', to: 'users/sessions#destroy', as: :destroy_user_session

My question is, why do we even need the as-block / device_for block? Why not just define it directly? What is the function and benefit of the as-block?



Solution 1:[1]

RESTful routes for devise looks like: devise_for :users ...

In any case, the as is equivalent to the devise_scope one, and they are designed for specifying your custom routes.

Here is the documentation on this question.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael Arkhipov