更新到 rails 3.2.11 后,我注意到我的应用程序中的搜索表单存在问题。在看到“我们很抱歉,但出了点问题”之后。Heroku 上的消息,我检查了日志并注意到以下错误消息:
NoMethodError (undefined method `to_i' for ["2010"]:Array):
app/controllers/skis_controller.rb:29:in `index'
这是指显示我的搜索结果的索引控制器。我对实际搜索表单使用了部分搜索表单,其中包含几个 collection_selects。
索引操作中的第 29 行指的是 model_year(上面搜索中的“2010”)。当我从 collection_select 中删除 model_year 字段时,一切正常(搜索后没有错误消息)。这就是 model_year collection_select 的样子:
<%= collection_select(:ski, :model_year, @model_years.sort_by(&:model_year).reverse, :model_year, :model_year, {}, :multiple => true, :class => "chzn-select", :'data-placeholder' => "Enter Model Year") %>
这让我很烦恼,因为它在过去三周内一直运行良好。我最近更新到 rails 3.2.11 但我不肯定这是问题开始的时候(我知道我之前没有注意到它)。我查看了 collection_select 文档,一切似乎都很好。
model_year 以整数形式存储在数据库中,我已经通过 rails 控制台确认了这一点。
任何帮助或想法将不胜感激!谢谢
编辑 - 更新添加控制器代码:
def index
@companies = Brand.scoped
@ski_types = Ski.find(:all, :select => "DISTINCT ski_type")
@genders = Ski.find(:all, :select => "DISTINCT gender")
@names = Ski.find(:all, :select => "DISTINCT name")
@price_ranges = PriceRange.scoped
@model_years = Ski.find(:all, :select => "DISTINCT model_year")
@sorts = ["Price", "Rating", "Availability"]
if params[:ski].present? || params[:brand].present? || params[:price_range].present?
# Creates references for collection select
ski_type = params[:ski][:ski_type].reject(&:blank?)
gender = params[:ski][:gender].reject(&:blank?)
company = params[:brand][:company].reject(&:blank?)
name = params[:ski][:name].reject(&:blank?)
price_range = params[:price_range][:price_range]
model_year = params[:ski][:model_year].reject(&:blank?)
# raise ski_type.any?.inspect
@ski = Ski.new
@ski.ski_type = ski_type
@ski.gender = gender
@ski.name = name
@ski.model_year = model_year
@price_range = PriceRange.new
@price_range.price_range = price_range
@brand = Brand.new
@brand.company = company
skis = Inventory.search_price(price_range)
skis_refined = Ski.search_characteristics(ski_type, gender, company, name, model_year)
ski_ids2 = skis.map(&:id) & skis_refined.map(&:id)
@all_skis = Ski.where(:id => ski_ids2)
if params[:sort_by] == "Price Low to High"
@overlapping_skis = Kaminari.paginate_array(@all_skis.joins(:inventories).order("inventories.price ASC").uniq_by(&:id)).page(params[:page]).per(30)
elsif params[:sort_by] == "Price High to Low"
@overlapping_skis = Kaminari.paginate_array(@all_skis.joins(:inventories).order("inventories.price DESC").uniq_by(&:id)).page(params[:page]).per(30)
elsif params[:sort_by] == "Rating"
@overlapping_skis = Kaminari.paginate_array(@all_skis.joins(:reviews).order("reviews.average_review DESC").uniq_by(&:id)).page(params[:page]).per(30)
else
@overlapping_skis = @all_skis.joins(:brand).order("brands.company ASC, skis.model_year DESC, skis.name ASC").page(params[:page])
end
else
@overlapping_skis = Ski.joins(:brand).order("brands.company ASC, skis.model_year DESC, skis.name ASC").page(params[:page])
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @skis }
end
end
更新搜索方法:
def self.search_characteristics(ski_type, gender, company, name, model_year)
@skis = Ski.scoped
if ski_type.any?
@skis = @skis.where(:ski_type => ski_type)
end
if gender.any?
@skis = @skis.where(:gender => gender)
end
if company.any?
brand_object = Brand.where(:company => company)
@id_array = brand_object.map(&:id)
@skis = @skis.where(:brand_id => @id_array)
end
if name.any?
@skis = @skis.where(:name => name)
end
if model_year.any?
@skis = @skis.where(:model_year => model_year)
end
return @skis
end