New to ruby on rails, trying to get this url structure:
/about
/about/staff
/about/contact
/about/brazil
/about/brazil/staff
/about/brazil/contact
When I do:
rails generate controller about index
rails generate controller about staff
rails generate controller about contact
I set my routes to:
get "about", :to => "about#index"
get "about/staff", :to => "about#staff"
get "about/contact", :to => "about#contact"
And all is well, but I got stumped when I need to开发者_JAVA技巧 do the same for a third level for the brazil office
Should I just do
rails generate controller about brazil_index
rails generate controller about brazil_staff
rails generate controller about brazil_contact
get "about/brazil", :to => "about#brazil_index"
get "about/brazil/staff", :to => "about#brazil_staff"
get "about/brazil/contact", :to => "about#brazil_contact"
Or is there a cleaner way to accomplish this?
I think you want to call this rails generate controller About index staff contact
to generate AboutController
with three actions index
, staff
and contact
. Then you want to allow passing in id parameter as the second path element:
Then in config/routes.rb
:
get "about/index"
get "about/staff"
get "about/contact"
get "about/:id/index" => 'about#index'
get "about/:id/staff" => 'about#staff'
get "about/:id/contact" => 'about#contact'
When I check routes with rake routes
I see now:
$ rake routes
about_index GET /about/index(.:format) {:controller=>"about", :action=>"index"}
about_staff GET /about/staff(.:format) {:controller=>"about", :action=>"staff"}
about_contact GET /about/contact(.:format) {:controller=>"about", :action=>"contact"}
GET /about/:id/index(.:format) {:controller=>"about", :action=>"index"}
GET /about/:id/staff(.:format) {:controller=>"about", :action=>"staff"}
GET /about/:id/contact(.:format) {:controller=>"about", :action=>"contact"}
You can now request http://localhost:3000/about/brazil/staff and the value of params[:id] will be "brazil".
One good option for this is to seperate the routes by using a 'namespace' in your /config/routes.rb
file like this:
namespace :about do
match '/' => 'about#index'
match '/staff' => 'about#staff'
match '/contact' => 'about#contact'
match '/:country/staff' => 'about#staff'
match '/:country/contact' => 'about#contact'
end
If you then run rake routes
, you can see the routing that results:
/about(.:format) {:controller=>"about/about", :action=>"index"}
/about/staff(.:format) {:controller=>"about/about", :action=>"staff"}
/about/contact(.:format) {:controller=>"about/about", :action=>"contact"}
/about/:country/staff(.:format) {:controller=>"about/about", :action=>"staff"}
/about/:country/contact(.:format) {:controller=>"about/about", :action=>"contact"}
So all these route to the same controller (which I believe is what you wanted), and you have only three actions: staff
, index
and contact
. The :country
value will be passed in as a parameter if it exists in the url and would be accessed as params[:country]
.
Is this what you were trying to do?
精彩评论