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

How do I add a new route (URL)? Question

+1
−0

I was thinking about taking a try at an issue that would add a new sub-page to the user profile. I understand that as part of this I need to add a "route" for the new URL and somehow wire that up to the view that I will define.

I'm new to that part of Ruby; so far I have only modified existing views, not created new ones. How does that work?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

This answer may help as a start (5 comments)

1 answer

+1
−0

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:

scope  'users' do

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

    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:

    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.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »