What is the "_path" method in ruby ​​on rails?

I am learning RoR, and the "_path" method, which is used in controllers and routes, is very confusing. To be more specific, I mean a lot of different calls that take the syntax "(something) _path". As far as I know, they all either encode or manipulate a URL or link. It’s hard for me to learn to use this type of method, because I can’t understand what its main functionality is.

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

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

Only now can I use the following in the .erb file. Note 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 the edit page (and another action) in another .erb file:

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

I tried reading my training materials as well as the RoR documentation, but I always lose more than when I started. Does anyone know the low-level definition of this method "_path"?

+6
source share
3 answers

Assistant

It's called route helper , which means Rails will generate them to help you create resource-based routing structures. I will tell more in a second

-

To explain correctly, Rails is just a framework.

Like all software, this is a series of files downloaded in a specific order. Thus, Rails creates a series of helper methods during the loading process. These “helper” methods can then be used throughout the application to invoke functionality / information as needed:

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

In addition to using standard template helpers, creating custom helpers to extract complex logic or reusable functionality is highly recommended. By default, each controller will include all helpers. These helpers are only available on the controller through .helpers

Route helpers, which are generated from your config/routes.rb , give you the ability to name routes that are resourceful. This may seem strange, but once you understand them, it will help you inexorably.

-

Inventive

To give you more clarity - Rails routes are called resourceful.

This means that they are built around resources. To give you a brief definition of this, you must understand that the resources of your application are data pools that you can add and pull from.

To explain further, since Rails is object oriented . If you are a beginner, this will not mean a lot, but remember this, because when you move on the language / work, you will begin to understand why this is important.

Object-oriented programming places objects in the center of the stream. Typically, you put logic in the center, but with OOP, these are objects. This is very important to us, as it means that everything you do in Rails is based on objects that you can create.

In accordance with the principle

This means that if you want to create a series of routes for the "CRUD" (Create Read Update Destroy) of your objects, Rails will be able to create the routes necessary for this. Here the resources directives come from the routes file:

enter image description here

Hope this helps!

+9
source

In fact, these paths are based on routes.rb . If you run this command in your project, you can see all available in your application.

 rake routes 

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

 resources :posts 

then I would automatically have the following available paths

 posts_path post_path new_post_path edit_post_path 

If you use some strange abc_path that was not declared in routes.rb , you will get an error.

I hope this is useful, you will definitely need to work more with Rails, and then, in the end, you will understand all these things :)

+7
source

you can find the definition for these methods in the rails repository:

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

0
source

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


All Articles