1

I really don't understand how to link to another view in rails.

I load datas in /products/index.html.erb and want to retrieve product id form one product to point to another view : subjects/index.html.erb

what is the correct syntax to have this url with the link_to : '/products/9/subjects' (for product id 9) ?

Many thanks !

Muntasim
  • 6,689
  • 3
  • 46
  • 69

4 Answers4

0

If you have nested routes defined as your question seems so:

<%= link_to "Click", [@product, :subjects] %>
Muntasim
  • 6,689
  • 3
  • 46
  • 69
  • 1
    Ok found the solution, i replace @product by p because i was in a loop :`@products.each do |p| and now works !!! ` Thank you so much – CodeRevenger Feb 17 '14 at 11:23
0
link_to "Click", subjects_product_path(9)

If you use rake routes it'll tell you which routes you have available you'd need nested resources for this to work

resources :products do
  resources :subjects
end
j-dexx
  • 10,286
  • 3
  • 23
  • 36
0

In your case:

<%= link_to "The name of your link", product_subjects_path(9) %>

Where 9 is the id of your product. You can use more generic link like:

<%= link_to "The name of your link", product_subjects_path(@product) %>

in a show view of a product, or even:

<h2>Links to subjects for each product </h2>

<% @products.each do |product| %>
    <%= link_to product.title, product_subjects_path(product) %>
<% end %>

in an index.

Assuming that in your routes you have subjects nested under products:

resources :products do
  resources :subjects
end

Don't forget when you are lost with your routes:

bundle exec rake routes

will give you all existing routes.

Here is the rails guide about routing with nested: doc

Here is the documentation for the syntax of the path: doc

coding addicted
  • 3,422
  • 2
  • 36
  • 47
  • <%= link_to "The name of your link", product_subjects_path(9) %> Returns an error [@product, :subjects] works it's really complicated to understand when we are newbie ! Why we use [@product, :subjects] in this syntax ?? – CodeRevenger Feb 17 '14 at 11:47
  • Assuming you are using @products.each do |p| can you try <%= link_to "The name of your link", product_subjects_path(p) %> This is the syntax i usually use in my app, i'll check for the another notation – coding addicted Feb 17 '14 at 11:56
  • Here is a doc for path and url, you'll find the short version of a link with the helper ( [p, :subjects] in your case ): [link](http://guides.rubyonrails.org/routing.html#creating-paths-and-urls-from-objects). It's just a shorter way to write the link_to. – coding addicted Feb 17 '14 at 12:11
0

To see all the routes in your rails application just use the following command:

rake routes

It will list all the routes along with the routes variables and the controller/action that they point to. From there you take the name of the route and append an _path or _url (in case you neeed an absolute url) in roder to obtain the name of the method to use in the link_to.

In your case the link_to for the /products/9/subjects path is:

link_to 'Link text', product_subjects_path(@product)
Ilie NEACSU
  • 530
  • 3
  • 12