在Rails中,show方法没有被执行

时间:2012-12-14 09:44:21

标签: ruby-on-rails ruby ruby-on-rails-3

Frnds我创建了一个控制器,名为products_controller和product model以及index.html.erb and show.html.erb 但是当我打电话给localhost:3000/products时,它会从数据库中获取详细信息。在同一页面中,我给出了一个链接,以清楚地显示产品的详细信息。但它没有加载。当我点击显示链接时,我必须获得每个产品的详细信息。但是localhost:3000 /产品),它加载到同一页面。 localhost的结果:3000 /产品

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>

3 个答案:

答案 0 :(得分:2)

首先,为什么你的模型没有id,你为什么要做原始SQL?

应该是:

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

根据您的观点,链接错误,您应该在迭代中传递当前产品的ID

<% @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将在幕后进行一些元编程,但它与做

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

答案 1 :(得分:0)

在我看来,您应该在<%= link_to 'Show', product %>

中使用<%= link_to 'Show', Product %>代替index.html.erb

答案 2 :(得分:0)

下面:

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

您需要使用product,这是块变量,而不是Product,这是一个类。