2

我在我的 Rails 3 项目中使用 MetaSearch gem。

我有两个模型:

class Company < ActiveRecord::Base
  belongs_to :city
end

class City < ActiveRecord::Base
  has_many :companies
end

我在 CompaniesController 中有操作:

def index
  @search = Company.search(params[:search])
  @companies = @search.all
end

该操作的视图包含:

= form_for @search do |f|
  = f.label :city_id_equals
  = f.select :city_id_equals
  = f.submit 'Search'

我想要一个包含城市名称的列表,并有机会按城市搜索公司。但是,我没有城市的名称和 ID,而是使用“City:0x00000102a20488”之类的内容,并且搜索无法正常工作。

我认为错误在这里:“:city_id_equals”。如何使它正确?

4

2 回答 2

5

The solution is found!

Instead of:

= f.label :city_id_equals
= f.select :city_id_equals

I should use:

= f.label :city_id_equals
= f.collection_select :city_id_equals, City.all, :id, :name, :include_blank => true
于 2011-07-07T07:08:57.587 回答
0

不确定你的问题是否真的很清楚。

首先,我猜你有类似的东西<City:0x00000102a20488>,它是 ruby​​ 对象的字符串表示形式。如果你想显示城市的名字,city.name应该做的伎俩(假设你在城市上有一个名字成员!)。

For the search, I don't really get what you are trying to do. What is :city_id_equals supposed to mean to you?

于 2011-07-06T15:14:11.773 回答