Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

60%
+1 −0
Q&A How do I add a new route (URL)?

posted 3mo ago by trichoplax‭  ·  edited 3mo ago by trichoplax‭

Answer
#2: Post edited by user avatar trichoplax‭ · 2024-09-12T18:48:50Z (3 months ago)
Typo
  • You can add a new route in the file `config/routes.rb`, and the existing routes in that file for the other "tabs" on the user profile page show the template to follow.
  • The relevant part of the routes file starts on line 180:
  • ```ruby
  • scope 'users' do
  • ```
  • Everything inside this `scope` block effectively has `users` prepended to its URL. For example, line 186 has:
  • ```ruby
  • get '/me', to: 'users#me', as: :users_me
  • ```
  • This lists the URL as `/me` but is actually triggered by the URL `users/me` due to being inside the `users` scope block.
  • Similarly line 200 has:
  • ```ruby
  • get '/:id', to: 'users#show', as: :user
  • ```
  • This is triggered by the URL `users/:id` (such as `users/1234`).
  • The other tabs on the user profile page have URLs of the form `users/:id/something` and can be found in this block listed as just `/:id/something` (or `/me/somethng` for the ones not visible to the public other than the user).
  • ## What the components mean
  • ### GET
  • The `get` at the beginning of the line means that this is triggered by a GET request (often used for visiting the page rather than making a background server request).
  • ### URL
  • The URL reflect what link the user followed, subject to having the scope (in this case `users`) prepended.
  • ### To
  • The `to: 'users#show'` is interpreted in both of two ways:
  • 1. This URL results in a call to the `show` method of the `UsersController`.
  • 2. The view rendered is the file `show.html.erb` in the directory `users`.
  • ### As
  • The `as: :user` is an optional renaming of the helper functions that Rails automatically creates for each route.
  • By default, the methods `users_show_path` and `users_show_url` would be created (matching `users#show`). The `as` overrides this to have the methods be called `user_path` and `user_url` instead.
  • You can add a new route in the file `config/routes.rb`, and the existing routes in that file for the other "tabs" on the user profile page show the template to follow.
  • The relevant part of the routes file starts on line 180:
  • ```ruby
  • scope 'users' do
  • ```
  • Everything inside this `scope` block effectively has `users` prepended to its URL. For example, line 186 has:
  • ```ruby
  • get '/me', to: 'users#me', as: :users_me
  • ```
  • This lists the URL as `/me` but is actually triggered by the URL `users/me` due to being inside the `users` scope block.
  • Similarly line 200 has:
  • ```ruby
  • get '/:id', to: 'users#show', as: :user
  • ```
  • This is triggered by the URL `users/:id` (such as `users/1234`).
  • The other tabs on the user profile page have URLs of the form `users/:id/something` and can be found in this block listed as just `/:id/something` (or `/me/somethng` for the ones not visible to the public other than the user).
  • ## What the components mean
  • ### GET
  • The `get` at the beginning of the line means that this is triggered by a GET request (often used for visiting the page rather than making a background server request).
  • ### URL
  • The URL reflects what link the user followed, subject to having the scope (in this case `users`) prepended.
  • ### To
  • The `to: 'users#show'` is interpreted in both of two ways:
  • 1. This URL results in a call to the `show` method of the `UsersController`.
  • 2. The view rendered is the file `show.html.erb` in the directory `users`.
  • ### As
  • The `as: :user` is an optional renaming of the helper functions that Rails automatically creates for each route.
  • By default, the methods `users_show_path` and `users_show_url` would be created (matching `users#show`). The `as` overrides this to have the methods be called `user_path` and `user_url` instead.
#1: Initial revision by user avatar trichoplax‭ · 2024-09-12T18:46:56Z (3 months ago)
You can add a new route in the file `config/routes.rb`, and the existing routes in that file for the other "tabs" on the user profile page show the template to follow.

The relevant part of the routes file starts on line 180:

```ruby
scope  'users' do
```

Everything inside this `scope` block effectively has `users` prepended to its URL. For example, line 186 has:

```ruby
    get  '/me',  to: 'users#me',  as: :users_me
```

This lists the URL as `/me` but is actually triggered by the URL `users/me` due to being inside the `users` scope block.

Similarly line 200 has:

```ruby
    get  '/:id',  to: 'users#show',  as: :user
```

This is triggered by the URL `users/:id` (such as `users/1234`).

The other tabs on the user profile page have URLs of the form `users/:id/something` and can be found in this block listed as just `/:id/something` (or `/me/somethng` for the ones not visible to the public other than the user).

## What the components mean
### GET
The `get` at the beginning of the line means that this is triggered by a GET request (often used for visiting the page rather than making a background server request).

### URL
The URL reflect what link the user followed, subject to having the scope (in this case `users`) prepended.

### To
The `to: 'users#show'` is interpreted in both of two ways:
1. This URL results in a call to the `show` method of the `UsersController`.
2. The view rendered is the file `show.html.erb` in the directory `users`.

### As
The `as: :user` is an optional renaming of the helper functions that Rails automatically creates for each route.

By default, the methods `users_show_path` and `users_show_url` would be created (matching `users#show`). The `as` overrides this to have the methods be called `user_path` and `user_url` instead.