【发布时间】:2016-08-15 16:37:33
【问题描述】:
这不是作业。我这样做是为了提高我的 RoR 技能。
我同时拥有Manufacturer 和Model 实体。我正确设置了所有关系,用户必须在选择Model 之前选择Manufacturer。我为自己创建的应用程序添加了更多功能。现在,我想从Model 下拉列表中过滤掉name,其中name 包含单词(Other)。
Model 实体中的name 属性示例:F20、F10 (Other)、E90。
我想过滤掉包含(Other) 的模型名称。 SQL 语句为Model.where.not("name LIKE ?", "%(Other)%")。选择manufacturer 后,将显示F20 和E90。 F10 (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)
【问题讨论】:
-
您在哪一行得到错误?请发布错误的堆栈跟踪
-
我的猜测:括号可能是需要转义的特殊字符???
-
@TarynEast 我不这么认为。我在
index.html.erb中使用Model.where.not("name LIKE ?", "%(Other)%"),并且在没有转义字符的情况下工作得很好。 -
@p4sh4 添加了错误代码,我不确定这是否是您的意思。
-
在您的服务器日志中(例如
log/development.log),您将能够找到应用程序中发生的所有事情的列表...包括您遇到的错误(type error)和紧随其后的是一大组 20-30 行,看起来像带数字的文件名。该集合称为堆栈跟踪,它准确地向您显示导致错误的代码行。看看其中的前 3-4 行对我们(以及您的扩展)会有所帮助:)
标签: sql ruby-on-rails ruby