简体   繁体   English

Meta_Search未定义方法错误

[英]Meta_Search undefined method error

Intro: I am working on a Car dealer application so I have a table named cars with fields carname_id and carmodel_id. 简介:我正在使用汽车经销商应用程序,因此我有一个名为cars的表,其字段为carname_id和carmodel_id。 carname_id and carmodel_id are taken from 2 tables named carnames and carmodels where I keep all car names and car models. carname_id和carmodel_id取自2个名为carnames和carmodels的表,其中保存了所有汽车名称和汽车模型。 Now I've setup these models so I can have them in a relation, something like belongs_to and has_many (not relevant cause this is working as it should be - I can add cars and models to a new car from a select list) 现在,我已经设置了这些模型,以便可以将它们关联起来,例如belongs_to和has_many(无关紧要,因为它应该正常工作-我可以从选择列表中将汽车和模型添加到新汽车中)

I am using meta_search to search by many fields and the code in the search_form looks like his 我正在使用meta_search来按许多字段进行搜索,并且search_form中的代码看起来像他的

<%= f.label :make_contains, "Select Car Make" %>
<%= f.select :make_contains, Car::CAR_MAKE %>

<%= f.label :model_contains, "Select Model" %>
<%= f.text_field :model_contains, Car::CAR_MODEL %>

the make and model fields are the old fileds in the cars table that I used for storing the car and model name of a car. 品牌和型号字段是汽车表中用于存储汽车和汽车型号名称的旧文件。 But when I get in to the advanced search form I found myself stuck in choosing dynamicaly the model list.. I have in car.rb a list of car names specified for Car list and a list for car models.. I agree that this is not the best way to do it, that's why I had to create 2 tables: carnames and carmodels that I've told you about at the start.. I have setup everything and the add new car works as expected, I can select a carname_id from carnames table and carmodel_id from carmodels table and it is stored in database perfectly using a form like this: 但是,当我进入高级搜索表单时,发现自己陷入了动态选择模型列表的问题。.在car.rb中,有一个为Car list指定的汽车名称列表和一个为car models列出的汽车..我同意这是不是最好的方法,这就是为什么我必须创建2个表的原因:一开始就告诉过您的汽车名称和汽车模型。我已经设置了所有内容,并且添加新汽车的工作符合预期,我可以选择carname_id来自carnames表和来自carmodels表的carmodel_id,它使用如下形式完美地存储在数据库中:

<%= f.label :carname_id, "Car name:" %>
<%= f.select :carname_id, @select_carnames %>

<%= f.label :carmodel_id, "Model name:" %>
<%= f.select :carmodel_id, @select_carmodels %>

this way I give up on make and model fields from cars table as I have 2 new working fields carname_id and carmodel_id 这样我就放弃了cars表中的make和model字段,因为我有2个新的工作字段carname_id和carmodel_id

Now to The Problem: how can I make the search by carmodel_id and carname_id, when making a form like this one: 现在到问题:创建这样的表单时,如何通过carmodel_id和carname_id进行搜索:

<%= f.label :carname_id_contains, "Model name:" %>
<%= f.select :carname_id_contains, @select_carnames %>
<%= f.label :carmodel_id_contains, "Model name:" %>
<%= f.select :carmodel_id_contains, @select_carmodels %>

returns me an error 给我一个错误
undefined method `carname_id_contains' for MetaSearch::Searches::Car:0xb584b7a4 未定义的方法“ carname_id_contains”用于MetaSearch :: Searches :: Car:0xb584b7a4

cars controller: 汽车控制器:

def index
  @search = Car.search(params[:search])
  @cars = @search.all.paginate :page => params[:page], :per_page => 18
  @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}
  @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @cars }
      format.json { render :json => @cars.map(&:attributes) }
    end
  end



   def new
    @car = Car.new
    @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}
    @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}
        5.times { @car.assets.build }

        respond_to do |format|
          format.html # new.html.erb
          format.xml  { render :xml => @car }
        end

  end

def edit
  @car = Car.find(params[:id])}
  @select_carnames = Carname.find(:all).map{|element| [element.name, element.id]}}
  @select_carmodels = Carmodel.find(:all).map{|element| [element.name, element.id]}}<br />
  5.times { @car.assets.build }
end

I hope I described it as good as possible and I really would apreciate any help and any ideas on this one. 我希望我描述得尽可能好,并且我真的很希望能对此提供任何帮助和想法。 Thanks all for your time. 感谢您的宝贵时间。

and the answer is 答案是

<%= f.label :carname_id_equals, "Model name:" %>
<%= f.collection_select :carname_id_equals, Carname.order('name ASC').all,  :id, :name, :include_blank => true %><br />

<%= f.label :carmodel_id_equals, "Model name:" %>
<%= f.collection_select :carmodel_id_equals, Carmodel.order('name ASC').all, :id, :name, :include_blank => true %><br />

I'll need to read more the Rdoc in the future :) 以后我需要阅读更多Rdoc :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM