0

Frnds 我创建了一个控制器,名为 products_controller 和产品模型,index.html.erb and show.html.erb 但是当我调用localhost:3000/products它时,我从数据库中获取详细信息。在同一页面中,我提供了一个链接,可以清楚地显示产品的详细信息。但它没有加载。当我点击显示链接时,我必须获取每个产品的详细信息。但是 localhost:3000/products),它会加载到同一页面。本地主机的结果: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

产品.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>

显示.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>
4

3 回答 3

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>
于 2012-12-14T09:52:53.477 回答
0

这里:

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

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

于 2012-12-14T09:52:06.170 回答
0

在我看来,你应该使用<%= link_to 'Show', product %>而不是<%= link_to 'Show', Product %>inindex.html.erb

于 2012-12-14T09:50:42.200 回答