Shub
Shub

Reputation: 265

Displaying only one record in the table

I want to display only a single row in the table. My controller for the same looks like this:

def Add
    @item= Item.find(params[:id])
end

and my table row looks like this.. Isn't there a substitute for the method .each to just display one record?

<% @item.each do |item| %>
<tr>
   <td><%= item.name %></td>
   <td><%= item.price %></td>
   <td><%= item.quantity %></td>
   <td><%= item.quantity * item.price %></td>
   <td>
      <% link_to("update", '#', :class => 'action update') %>
      <% link_to("X", '#', :class => 'action delete') %>
   </td>
</tr>
<% end %>

Upvotes: 0

Views: 90

Answers (2)

Lucky
Lucky

Reputation: 140

@item= Item.find(params[:id])

Here, @item will be an object of class Item. So You don't need to Iterate the Object with 'each'. You can directly access the Item attribute by @item object.

@item.name
@item.price
@item.quantity.....

Upvotes: 1

Jeet
Jeet

Reputation: 1380

In this case you don't need loop. U can directly create table like this

<tr>
  <td><%= @item.name %></td>
  <td><%= @item.price %></td>
  <td><%= @item.quantity %></td>
  <td><%= @item.quantity * @item.price %></td>
  <td>
    <% link_to("update", '#', :class => 'action update') %>
    <% link_to("X", '#', :class => 'action delete') %>
  </td>
</tr>

Upvotes: 2

Related Questions