我有一个我敢肯定的问题......
我有一个视图,其中显示了交付给客户的产品列表。我使用了几个 Ryan Bates 的 railscasts 作为模型,用于具有搜索字段和可排序的列以及分页。
分页和搜索工作正常。但是,我有两个我似乎找不到的问题:
1) 如果我尝试为产品显示客户的姓名,而不是他们的 ID,我会收到一条错误消息,指出未定义“名称”方法。
2)当我单击列标题以更改排序顺序时,没有任何反应。
这是我的代码:
产品控制器:
class ProductsController < ApplicationController
# GET /products
# GET /products.json
helper_method :sort_column, :sort_direction
def index
@products = Product.search(params[:search]).order(sort_column + " " + sort_direction).paginate(:per_page => 25, :page => params[:page])
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
end
end
.
.
.
private
def sort_column
Product.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
产品型号:
# == Schema Information
#
# Table name: products
#
# id :integer(4) not null, primary key
# name :string(255)
# serial_number :string(255)
# date_ordered :date
# date_received :date
# date_invoiced :date
# vendor_invoice :string(255)
# scc_invoice :string(255)
# quantity :integer(10)
# dollar_amount :integer(10)
# warranty_years :integer(4)
# warranty_days :integer(4)
# item :integer(4)
# comments :text
# created_at :datetime not null
# updated_at :datetime not null
# vendor_id :integer(4)
# customer_id :integer(4)
# category_id :integer(4)
# department :string(255)
#
class Product < ActiveRecord::Base
attr_protected :id
belongs_to :customer
belongs_to :vendor
belongs_to :category
def self.search(search)
if search
where('name LIKE ?', "%#{search}%")
else
scoped
end
end
end
产品索引视图:
<% title "Products" %>
<%= form_tag products_path, :method => 'get', :id => "products_search" do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil%>
</p>
<div id="products"><%= render 'products' %></div>
<% end %>
<p><%= link_to 'Add Product', new_product_path %></p>
产品部分视图:_products.html.erb
<%= hidden_field_tag :direction, params[:direction] %>
<%= hidden_field_tag :sort, params[:sort] %>
<table class="pretty">
<tr>
<th><%= sortable "name" %></th>
<th><%= sortable "serial_number" %> </th>
<th><%= sortable "date_invoiced" %></th>
<th>Scc invoice</th>
<th>Customer</th>
<th></th>
<th></th>
</tr>
<% for product in @products %>
<tr>
<td><%= product.name %></td>
<td><%= product.serial_number %></td>
<td><%= product.date_invoiced %></td>
<td><%= product.scc_invoice %></td>
<td><%= product.customer.name %></td>
<td><%= link_to 'Show', product %></td>
<td><%= link_to 'Edit', edit_product_path(product) %></td>
</tr>
<% end %>
<%= will_paginate @products %>
我不确定其他人可能需要什么来了解正在发生的事情,所以如果需要更多信息,请告诉我。
这是我在服务器运行时看到的内容:
在 2012-06-27 14:04:11 -0600 开始 GET "/products" for 127.0.0.1 连接到由 database.yml 指定的数据库 由 ProductsController#index 处理为 HTML ←[1m←[36mProduct Load (60.0ms)← [0m ←[1mSELECT products
.* FROM products
ORDER BY name asc LIMIT 25 OFFSET 0←[0m ←[1m←[35m (0.0ms)←[0m SELECT COUNT( ) FROM products
←[1m←[36mCustomer Load (0.0ms)←[ 0m ←[1mSELECT customers
. 从哪里来。customers
_ = 0 LIMIT 1←[0m 渲染产品/_products.html.erb (535.0ms) 在布局/应用程序中渲染产品/index.html.erb (660.0ms) 895ms 内完成 500 内部服务器错误customers
id
ActionView::Template::Error ( name' for nil:NilClass):
20: <td><%= product.serial_number %></td>
21: <td><%= product.date_invoiced %></td>
22: <td><%= product.scc_invoice %></td>
23: <td><%= product.customer.name unless product.customer.name.nil? %></td>
24: <td><%= link_to 'Show', product %></td>
25: <td><%= link_to 'Edit', edit_product_path(product) %></td>
26: </tr>
app/views/products/_products.html.erb:23:in
_app_views_products__products_html_erb__429528032_35444568' app/views/products/_products.html.erb:16:in _app_views_products__products_html_erb__429528032_35444568'
app/views/products/index.html.erb:8:in
block in _app_views_products_index_html_erb_27575616' app/views/products/index.html.erb' _app_views_products_index_html_erb___506300995_27575616'
app/controllers/products_controller.rb:11:in
: in block in
渲染 C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescues/_trace.erb (5.0ms) 渲染 C:/ RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescues/_request_and_response.erb (5.0ms) 渲染 C:/RailsInstaller/Ruby1。救援/布局中的 9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescues/template_error.erb (65.0ms)