Surender Singh
Surender Singh

Reputation: 57

How can i display a particular product?

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

Upvotes: 1

Views: 294

Answers (4)

Kamal Pandey
Kamal Pandey

Reputation: 1598

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

Upvotes: 0

kishore cheruku
kishore cheruku

Reputation: 521

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".

Upvotes: 0

Vishal
Vishal

Reputation: 7361

Instead of this

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

Use rails standard way

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

Upvotes: 3

oj5th
oj5th

Reputation: 1399

products_path(@product) should be products_path(product)

Upvotes: 0

Related Questions