Reputation:
I see in routes.rb like this
%w( about mission path standard getting_started welcome infection instruction implementation ).each do |page|
get page, to: "pages##{page}"
end
And when I see home
controller , it doesn't have nay actions which list above. But the link works properly.
I want to know what these lines of codes do?
Upvotes: 0
Views: 370
Reputation: 2786
%w( about mission path standard getting_started welcome infection instruction implementation ).each do |page|
get page, to: "pages##{page}"
end
The above code is defining actions which belong to the pages controller and not the home controller.
Hope that information help you to figure out the things.
Upvotes: -1
Reputation: 5380
To my understanding the actual question is how is this working when no actions are defined in home controller
.
The route you are describing just defines a number of different actions that can be run against your home controller
, like about
, mission
etc.
In order for those pages to be displayed you only need a relevant view in app/views/pages/about.html.erb
in order for it to be displayed.
There is no need to define actions in controller like def about
to display views. You would need to define them ONLY if you want to set some variables for them to be displayed in the view like @my_name
, but by definition you don't need to have controller actions in order to display them
Upvotes: 0
Reputation: 285
%w( about mission path standard getting_started welcome infection instruction implementation ).each do |page|
get page, to: "pages##{page}"
end
The code works like: %w(foo bar)
is a shortcut for array["foo", "bar"]
.each do |page|
It loops each element such as in 1st loop the value of page = "foo"
get page, to: "pages##{page}"
This line will become
get foo, to: "pages#foo"
when user hits /foo you will be redirected to foo action of pages controller, this will be same for other elements too.
Thus, this makes easy to define routes for all the elements in %w( )
Upvotes: 4
Reputation: 11
If it works fine, then maybe you should look for the page
controller, not the home
controller.
part:
%w( about mission path standard getting_started welcome infection instruction implementation )
%w( - works only for array of strings and is just another way to write:
['about', 'mission', 'path', 'standard', 'getting_started', 'welcome', 'infection', 'instruction', 'implementation']
It's more convenient, because you don't have to worry about commas and other, just separate items of array by space.
If you iterate (.each) this and in block you do get page, to: "pages##{page}"
it does for every item:
get 'about', to: "pages#about"
get 'mission', to: "pages#mission"
and so on. And in this case controller is 'pages', and action is 'about' etc.
Here is more about %w: http://ruby-doc.org/core-2.2.3/doc/syntax/literals_rdoc.html#label-Percent+Strings
About routing: http://guides.rubyonrails.org/routing.html#singular-resources
Upvotes: 1
Reputation: 2020
the %w is a ruby processor which splits the inputted string at the whitespace and outputs an array.
then it applies the
get page, to: "pages##{page}"
to the specific entries in the array.
The reason you don't see the actions defined in the PagesController is because the controller actions would probably be empty since they are probably semi-static pages.
In this case the Ruby on Rails application only requires the correct views to be in place and no specific action to be defined.
Upvotes: 0