Nabe Nabe - 4 months ago 10
Ruby Question

Ruby: why use # not /?

In the following, what is the reason for having

#home
rather than
/home
? All others are
/help
,
/about
.

Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end

Answer

This is the full equivalent of the code you provided:

Rails.application.routes.draw do
  get '/',                  to: 'static_pages#home'
  get 'static_pages/help',  to: 'static_pages#help'
  get 'static_pages/about', to: 'static_pages#about'
end

The hash symbol # divides controller and action names in route destination string.

In your example you use the root method. It is a shorthand for get '/'. So you don't specify the request path.

In the last two rules, destination can be omitted because Rails calculates it automatically.

I suggest that you read the official Rails routing guide. It explains all these things.

Comments