Meta_Search 未定义方法错误

发布于 2024-12-08 13:30:52 字数 2771 浏览 0 评论 0原文

简介: 我正在开发一个汽车经销商应用程序,因此我有一个名为 cars 的表,其中包含字段 carname_id 和 carmodel_id。 carname_id 和 carmodel_id 取自 2 个名为 carnames 和 carmodels 的表,我在其中保存所有汽车名称和汽车型号。现在我已经设置了这些模型,这样我就可以将它们建立在某种关系中,例如belongs_to和has_many(不相关,因为它应该按预期工作 - 我可以从选择列表中将汽车和模型添加到新车中)

我是使用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 %>

品牌和型号字段是汽车表中的旧字段,我用于存储汽车和汽车的型号名称。但是当我进入高级搜索表单时,我发现自己陷入了动态选择型号列表的困境。我在 car.rb 中有一个为汽车列表指定的汽车名称列表和汽车型号列表。我同意这是这不是最好的方法,这就是为什么我必须创建 2 个表:我在开始时告诉过你的 carnames 和 carmodels 。我已经设置了所有内容,并且添加新车按预期工作,我可以选择 carname_id来自 车名carmodels 表中的 table 和 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 %>

这样我就放弃 cars 表中的 make 和 model 字段,因为我有 2 个新的工作字段 carname_id 和 carmodel_id

现在解决问题: 在制作这样的表单时,如何通过 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 %>

返回错误
的未定义方法“carname_id_contains”

MetaSearch::Searches::Car:0xb584b7a4汽车控制器

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

:我希望我尽可能好地描述它,我真的很感激对此的任何帮助和任何想法。感谢大家抽出时间。

Intro:
I am working on a Car dealer application so I have a table named cars with fields carname_id and 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. 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)

I am using meta_search to search by many fields and the code in the search_form looks like his

<%= 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:

<%= 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

Now to The Problem:
how can I make the search by carmodel_id and carname_id, when making a form like this one:

<%= 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尾戒 2024-12-15 13:30:52

答案是

<%= 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 />

我将来需要阅读更多 Rdoc :)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文