user3736091
user3736091

Reputation:

What does xxxx_path mean in ruby on rails

I try to add new controller and model use name foo and foos_controller, hope foos_path can redirect. Doesn't work.

A origin code here (working):

href="<%= contacts_path %>"

After I add new controller and model follow name convention I try use the same (Not working):

href="<%= foos_path %>"

And this contacts_path is not defined anywhere else in rb project. what does xxxx_path mean and how to use it?

Upvotes: 1

Views: 93

Answers (3)

Richard Peck
Richard Peck

Reputation: 76774

You'll be best reading up on this documentation


Path Helpers

Basically, the path_helpers of Rails are designed to help you define routes for your links etc in the most efficient way possible.

The bottom line is the path helper will take routes defined in your config/routes.rb and then allow you to call them dynamically - IE:

#config/routes.rb
resources :photos #-> photos_path

The path names are typically from your controllers, allowing you to route to the various actions inside them. As Rails is built around being a resourceful structure, it will by default create routes for the "standard" controller actions:

enter image description here


link_to

In order to use the path helpers effectively, you'll be best using the rake routes command in cmd, or by simply typing an invalid url into your app's address bar

I notice you're using the standard HTML <a href=""> tag in your question. You'll be much better suited to using the link_to helper of Rails:

<%= link_to "text", your_path %>

Upvotes: 0

Ege Ersoz
Ege Ersoz

Reputation: 6571

If you go to your terminal and type rake routes, it will list your currently defined routes. On the left side you'll have their prefix. For example, contacts might route to the index action in ContactsController.

The path suffix is a way of referring to those routes inside your code.

If foos_path is giving you an error, that means you either have not yet defined that route for the resource, or you have but it is called something else (if you defined it manually with the as: option).

More info can be found in the Rails Guide for Routing.

Upvotes: 0

anusha
anusha

Reputation: 2135

Rails follows convention to handle roots of application

when we execute this command

rails g scaffold foo

it generates routes along with your model, controller and views.

it generates a line in routes.rb as

 resources :foo

this line makes you access all the actions of your controller

for example:

foos_path: # redirects you to the  index page of your foos controller
new_foo_path: # redirects you to the create page of your foos controller etc.,

please go through this link for reference: http://guides.rubyonrails.org/routing.html

Upvotes: 1

Related Questions