顯示公會我有什麼,我相信是一個nooby問題...排序的列,並在指數佈局
我有哪裏我顯示的交付給客戶的產品列表的視圖。我使用了一些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 %>
我不知道還有什麼人可能需要得到的東西是對的想法,所以讓我知道如果有需要了解更多信息。
這是我在服務器上看到,當它運行:
開始GET「/產品」爲127.0.0.1於2012-06-27 14點04分11秒-0600 通過數據庫中指定連接到數據庫.yml Products by ProductsController#索引爲HTML ←[1m←[36mProduct Load(60.0ms)←[0m←[1mSELECT products
。* from products
ORDER BY名稱asc LIMIT 25 OFFSET 0←[0m ←[1m←[ 35m(0.0ms)←[0m SELECT COUNT()從products
←[1m←[36mCustomer Load(0.0ms)←[0m←[1mSELECT customers
。 FROM customers
WHERE customers
。 id
= 0 LIMIT 1←[0米 提煉產品/ _products.html.erb(535.0ms) 提煉產品/佈局內index.html.erb /應用(660.0ms) 完成500內部服務器錯誤在895ms
::的ActionView模板::錯誤(未定義的方法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' 應用/視圖/產品/ _products.html.erb:16:在_app_views_products_index_html_erb___506300995_27575616' _app_views_products__products_html_erb__429528032_35444568' app/views/products/index.html.erb:8:in
塊 應用/視圖/產品/ index.html.erb:3 :在_app_views_products_index_html_erb___506300995_27575616' app/controllers/products_controller.rb:11:in
索引'
渲染C:/ RailsInstalle r/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescue/_trace.erb(5。0ms) Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescue/_request_and_response.erb(5.0ms) Rendered C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/actionpack-3.2.5/lib/action_dispatch/middleware/template s/rescue/template_error.erb within rescues/layout(65.0 MS)從堆棧跟蹤
請檢查您的瀏覽器JavaScript控制檯,當您點擊列標題並告訴我們您看到的情況時會發生什麼。對於Firefox來說,這將使用螢火蟲。 –
看起來是排序方向不在ASC和DESC之間來回切換。它每次點擊列標題時都會顯示'ASC'。 – jhstephenson