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

Comments on Cannot find definition of function that returns a path

Parent

Cannot find definition of function that returns a path Question

+1
−0

I want to find the place in the codebase where a path is defined, but searching for the function that creates it gives no results apart from the usage of that function.

For example, see thread.html.erb, line 142:

<%= link_to 'See the whole thread', comment_thread_path(@comment_thread) %>.

This calls a function comment_thread_path but searching for this function finds no definition of it.

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?

0 comment threads

Post
+1
−0

The function comment_thread_path is created automatically by Rails, based on the information provided in config/routes.rb.

See Rails Routing from the Outside In for further details.

Specifically, line 236 of config/routes.rb says (enclosing scope included for context):

scope 'comments' do
    ...
    get 'thread/:id', to: 'comments#thread', as: :comment_thread
    ...
end

This responds to a GET request on the route /comments/thread/:id by:

  • First calling the thread method of the CommentsController
  • Then rendering the app/views/comments/thread.html.erb view.

See Action Controller Overview for further details.

The comments#thread in the routes file determines both:

  • Which controller and method to call (being in the format controller#method)
  • Which view to render and where to find it (being in the format directory#viewfile)

The optional as: comment_thread overrides the default name that Rails would otherwise use in its automatically generated helper functions (otherwise it would use the default comments_thread - note the pluralisation). Now comment_thread_path and comment_thread_url are available, each a function taking a thread id as argument:

  • Calling comment_thread_path(123) returns /comments/thread/123
  • Calling comment_thread_url(123) returns the same thing but including the domain at the beginning, such as example.com/comments/thread/123.
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

Thank you! I've gotten confused by routes too. (2 comments)
Thank you! I've gotten confused by routes too.
Monica Cellio‭ wrote 6 months ago

Thank you! I've gotten confused by routes too.

trichoplax‭ wrote 6 months ago

I didn't write this up the first time it confused me, but having been confused again months later, I thought writing it up might help it stick in my memory...