analyze_collection_select

时间:2016-08-15 05:24:22

标签: sql ruby-on-rails ruby

这不是作业。我这样做是为了提高我的RoR技能。

我有ManufacturerModel实体。我已正确设置所有关系,用户必须在选择Manufacturer之前选择Model。我为我创建的应用程序添加了更多功能。现在,我想从name下拉列表中过滤掉Model,其中name包含单词(Other)

name实体中Model属性的示例:F20F10 (Other)E90

我想过滤掉包含(Other)的模型的名称。 SQL语句为Model.where.not("name LIKE ?", "%(Other)%")。选择manufacturer后,系统会显示F20E90F10 (Other)将被排除,因为该名称包含单词(Other)

这就是我对下拉列表表格的看法。

<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
    <div class="field">
        <%= f.collection_select :manufacturer_id, Manufacturer.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %> 
    </div>

    <div class="field">
        <%= f.grouped_collection_select :model_id, Manufacturer.order(:name), :models, :name, :id, :name, {:prompt => "Select Model"} %> 
    </div>

    //Some code here that are not related
<% end %>

我尝试过: 在我的tints_controller中,我和我已经更新了我的表单,以便使用我修改过的内容。

  def quotation
    @listmanufacturer = Manufacturer.order(:name)
    @listmodel = Model.where.not("name LIKE ?", "%(Other)%")
    render 'quotation'
  end

我的表格

<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
    <div class="field">
        <%= f.collection_select :manufacturer_id, @listmanufacturer, :id, :name, {:prompt => "Select Manufacturer"} %> 
    </div>

    <div class="field">
        <%= f.grouped_collection_select :model_id, @listmanufacturer, @listmodel, :name, :id, :name, {:prompt => "Select Model"} %> 
    </div>

    //Some code here that are not related
<% end %>

但是,我收到了TypeError: nil is not a symbol之类的错误。我无法修改我的models_controller,因为下拉列表在不同的视图中被使用了几次。有些视图需要使用单词(Other)的模型,有些不需要。

@listmodel是打破一切的部分。通过将:models替换为@listmodel,我正在跳过筛选出包含(Other)的模型名称。

非常感谢任何帮助!

注意:到目前为止,我的应用程序运行正常。在model中正确设置了所有关系,并配置了routes

更新,更多日志

ActionView::Template::Error (#<ActiveRecord::Relation [#<Model id: 1, name: "Kancil Old", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 2, name: "Kancil 04", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 3, name: "Kembara", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 4, name: "Kenari", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 5, name: "Kelisa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 6, name: "Myvi 05", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 7, name: "Rusa", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 8, name: "Viva", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 9, name: "Alza", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, #<Model id: 10, name: "Myvi Ii", manufacturer_id: 1, created_at: "2016-08-14 16:12:24", updated_at: "2016-08-14 16:12:24">, ...]> is not a symbol nor a string):
     6:         </div>
     7: 
     8:         <div class="field">
     9:             <%= f.grouped_collection_select :model_id, @listmanufacturer, @listmodel, :name, :id, :name, {:prompt => "Select Model"} %> 
    10:         </div>
    11: 
    12:         <div class="field">

app/views/tints/quotation.html.erb:9:in `block in _app_views_tints_quotation_html_erb__1724914643_132263140'
app/views/tints/quotation.html.erb:3:in `_app_views_tints_quotation_html_erb__1724914643_132263140'
app/controllers/tints_controller.rb:47:in `quotation'
  Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout
  Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (9.0ms)
  Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.0ms)
  Rendering C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-5.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (539.0ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from status_code_with_paginate at C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/will_paginate-3.1.0/lib/will_paginate/railtie.rb:49)

1 个答案:

答案 0 :(得分:0)

在我的Manufacturer model

class Manufacturer < ApplicationRecord
    //Something here
    def listmodel 
        models.where.not("name LIKE ?", "%(Other)%")
    end 
end

在我的form

<%= bootstrap_form_tag url: '/quotation/tints/generate' do |f| %>
        <div class="field">
            <%= f.collection_select :manufacturer_id, Manufacturer.all.order(:name), :id, :name, {:prompt => "Select Manufacturer"} %> 
        </div>

        <div class="field">
            <%= f.grouped_collection_select :model_id, Manufacturer.all.order(:name), :listmodel, :name, :id, :name, {:prompt => "Select Model"} %> 
        </div>
        //More code here
<% end %>

现在,在我的下拉列表中,将排除名称中包含(Other)的模型。