Pavan Kumar
Pavan Kumar

Reputation: 1735

In Rails show method is not getting executed

Frnds I have created a controller, named products_controller and product model and also index.html.erb and show.html.erb but when i call localhost:3000/products it get the details from database. in the same page i have given a link to show the details of a product clearly. But it is not loading. when i clicks on show link i has to get the details of each product. but localhost:3000/products), it loads to the same page. result for localhost:3000/products

Model Name  Brand Name  Price   Discount    Qty Available           
Nokia Lumia     Nokia   15000   5.0     25  Show    
Samsung galaxy Nexus    Samsung     45000   15.0    5   Show    
Sony Experia    Sony Ericsson   4500    1.0     10  Show    
Nikon D5100     Nikon   25500   10.0    100     Show    
Panasonic Lumix     Panasonic   25500   10.0    104     Show    
Olympus Granite     Olympus     24520   12.0    45  Show    

products_controller.rb

class ProductsController < ApplicationController
  def index
    @products = Product.select("model_name,brand_name, price, discount, qty_avaliable")
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  #render :text => params and return false

  def show
    @product = Product.find_by_sql("select model_name,brand_name, price, discount, qty_avaliable from products where prod_id='PR1'")
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

end

product.rb

class Product < ActiveRecord::Base
  attr_accessible :prod_id, :model_name, :brand_name, :price, :discount, :qty_avaliable
end

index.html.erb

<p id="notice"><%= notice %></p>

<h1>Products  List</h1>

<table>
  <tr>
    <th>Model Name</th>
    <th>Brand Name</th>
    <th>Price</th>
    <th>Discount</th>
    <th>Qty Available</th>

    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @products.each do |product| %>
  <tr>
    <td><%= product.model_name %></td>
    <td><%= product.brand_name %></td>
    <td><%= product.price %></td>
    <td><%= product.discount %></td>
    <td><%= product.qty_avaliable %></td>
    <td><%= link_to 'Show', Product %></td>


  </tr>
<% end %>
</table>

show.html.erb

<p prod_id="notice"><%= notice %></p>

<p>
  <b>Model Name:</b>
  <%= @product.model_name %>
</p>

<p>
  <b>Brand Name:</b>
  <%= @product.brand_name %>
</p>

<p>
  <b>Price:</b>
  <%= @product.price %>
</p>

<p>
  <b>Discount:</b>
  <%= @product.discount %>
</p>

<p>
  <b>Quantity:</b>
  <%= @product.qty_avaliable %>
</p>

Upvotes: 0

Views: 129

Answers (3)

rorra
rorra

Reputation: 9693

First thing, why don't you have an id for your model, and why are you doing raw SQL?

It should be:

class ProductsController < ApplicationController
  def index
    @products = Product.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  def show
    @product = Product.where(id: params[:id]).first
    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

end

And on your views, the link is wrong, you should pass the id for the current product in the iteration

<% @products.each do |product| %>
  <tr>
    <td><%= product.model_name %></td>
    <td><%= product.brand_name %></td>
    <td><%= product.price %></td>
    <td><%= product.discount %></td>
    <td><%= product.qty_avaliable %></td>
    <td><%= link_to('Show', product) %></td>
  </tr>
<% end %>

link_to will do some meta programming behind the scenes, but its mostly the same than doing

    <td><%= link_to('Show', product_path(id: product.id) %></td>

Upvotes: 2

Matt
Matt

Reputation: 5398

Here:

<td><%= link_to 'Show', Product %></td>

You need to use product, which is the block variable, not Product, which is a class.

Upvotes: 0

Shamir K.
Shamir K.

Reputation: 395

It seems to me, that you should use <%= link_to 'Show', product %> instead of <%= link_to 'Show', Product %> in index.html.erb

Upvotes: 0

Related Questions