Rails 3 Routing Resources Associated with Username

I have a basic understanding of rail routing, but nothing too advanced. So far, I have gotten using resource-based RESTful routes and a few custom routes.

Now I have almost done my application, and I wanted to make some good urls.

In my application, each user has many pages. What is the best way to make the url look like www.sitename.com/username/page_name?

+3
source share
3 answers

This will display the controller action. The hash parameter includes: username and: page_name.

match "/:username/:page_name" => "pages#show"

Do not forget to put the latter, or it will correspond to almost everything.

+4
source

, , - :

resources :users do
    get 'page_name'
end

: users/:id/page_name

, Railsguide .

0

What you are looking for is a membership route (section 2.9.1).

resources :users do
  member do
    get :cool_page
  end
end

will result in / users /: id / cool_page

0
source

Source: https://habr.com/ru/post/1776279/


All Articles