I am trying to display a particular product by clicking on it from the list

<ul>
    <% products.each do |product| %>
    <tr>

        <a href="<%= products_path(@product) %>"><%= product.title %> .   </a>
        <br/>

    </tr>
    <% end %>
</ul>

Expected results should be products/1 but the result is products.1 and should displays a list of products

4 Answers

0
oj5th On

products_path(@product) should be products_path(product)

1
Vishal On

Instead of this

 <a href="<%= products_path(@product) %>"><%= product.title %> .   </a>

Use rails standard way

<%= link_to product.title, product_path(product)  %>
0
Kamal Pandey On

If you want to do in this way instead of

<ul>
    <% products.each do |product| %>
    <tr>

        <a href="<%= products_path(@product) %>"><%= product.title %> .   </a>
        <br/>

    </tr>
    <% end %>
</ul>

try

<ul>
    <% products.each do |product| %>
    <tr>

        <a href="/products/#{product.id}"><%= product.title %></a>
        <br/>

    </tr>
    <% end %>
</ul>

but in rails way you can do

<ul>
    <% products.each do |product| %>
    <tr>

        <%= link_to product.title, product_path(product) %>

        <br/>

    </tr>
    <% end %>
</ul>

or

<ul>
        <% products.each do |product| %>
        <tr>

            <%= link_to product.title, product %>

            <br/>

        </tr>
        <% end %>
    </ul>

and in routes you shoud specify

resources :products
0
user3678149 On

If your routes used resources :products , then you can use simply

No need to use any path. Just go with Product object.

Note : In controller method should be call "show".