24

I'm learning RoR, and I'm getting very confused by the "_path" method as it's used in Controllers and Routes. To be more specific, I'm referring to the many different calls that take the syntax "(something)_path". So far as I know, they all seem to either encode or manipulate a URL or link. I'm having a hard time mastering the use of this type of method because I can't figure out what it's core functionality is supposed to be.

For example, I could use the following code to redirect an old URL structure to a page of listed Tweet instances in my config/routes.rb file:

get '/all' => 'tweets#index', as: 'all_tweets'

Only now can I use the following in an .erb file. Notice the "_path" code at the end of the line.

<%= link_to "All Tweets", all_tweets_path %>

I could also use the following code to create a link to an edit page (and another action) in a different .erb file:

<p><%= link_to tweet.user.name, edit_tweet_path(@tweet) %></p>

I've tried reading through my study materials as well as the RoR documentation, but I always end up more lost than when I began. Does anybody know the low-level definition of this "_path" method?

3 Answers 3

29

Helper

It's called a route helper, which means that Rails will generate them to help provide you with resource-based routing structures. I'll explain more in a second

--

To explain properly - Rails is just a framework.

Like all software, it is a series of files loaded in a particular order. As such, Rails creates a series of helper methods in the booting process. These "helper" methods can then be used throughout your application to call functionality / information as you require:

The Rails framework provides a large number of helpers for working with assets, dates, forms, numbers and model objects, to name a few. These helpers are available to all templates by default.

In addition to using the standard template helpers provided, creating custom helpers to extract complicated logic or reusable functionality is strongly encouraged. By default, each controller will include all helpers. These helpers are only accessible on the controller through .helpers

The route helpers (which are generated from your config/routes.rb file give you the ability to call routes which are resourceful. These might seem strange to begin with, but once you understand them, will help you inexorably.

--

Resourceful

To give you more clarity - Rails routes are known as resourceful

This means they are constructed around resources. To give you a brief definition of this, you need to appreciate that the resources of your application are the pools of data you can add to, and pull from.

To explain further, because Rails is object orientated. If you're new, this won't mean very much, but keep it in mind, as when you progress through the language / work, you'll begin to see why this is important.

Object orientated programming puts OBJECTS at the center of the flow. Typically, you'd put logic at the center, but with OOP, it's the objects. This is very important for us, as it means that everything you do in Rails is based around the objects you can create.

As per the MVC principle (which, again, is what Rails is built on), you'll create / invoke your objects from your Models:

enter image description here

This means that if you want to create a series of routes to "CRUD" (Create Read Update Destroy) your objects, Rails is able to create the routes necessary to do that. This is where the resources directives come from inside the routes file:

enter image description here

Hope this helps!

0
20

Actually, these paths are generated based on your routes.rb. If you run this command at your project, you would be able to see all available on your app

rake routes

For example, if I declare my resources in routes.rb like this

resources :posts

then I would automatically have following available paths

posts_path
post_path
new_post_path
edit_post_path

If you use some strange abc_path which has not been declared in routes.rb, then you will get errors.

Hope this is helpful, you will definitely need to work more with Rails and then eventually you will understand all of these things :)

2
  • Thanks so much for making it simple for me. Are those four (list all, show one, create, update) the only four "_path" methods that get created?
    – elersong
    Commented Sep 13, 2014 at 5:48
  • Yeah, that's right. You can try putting resources :posts in your routes.rb and run rake routes to see how different methods are generated :) Commented Sep 13, 2014 at 10:06
2

you could find definition for these methods in rails repository:

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L127

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.