collection_select NoMethodError - 切换到rails 3.2.11后注意到

时间:2013-01-15 20:42:33

标签: ruby-on-rails view-helpers

更新到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 console确认了这一点。

非常感谢任何帮助或想法!感谢

编辑 - 更新以添加控制器代码:

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

1 个答案:

答案 0 :(得分:0)

你有:multiple => true,这意味着model_year可以有多个选定的值,这就是为什么它是一个数组。您必须以某种方式处理这些多个值,或删除:multiple => true