【问题标题】:Couldn't find Manufacturer with 'id'= (blank)找不到具有“id”=(空白)的制造商
【发布时间】:2016-07-22 15:09:39
【问题描述】:

我正在尝试创建一个 Web 应用程序来练习我的 Ruby on Rails 技能。我的数据库中有几个实体manufacturers, models, tints, prices

  • 制造商{id, name} - 存储汽车品牌
  • models {id, manufacturer_id, name} - 存储汽车的模型
  • tints {id, manufacturer_id, model_id, front, sides, rear} - 存储所需的色调长度
  • prices {id, description, price } - 存储商品的价格

我创建了一个页面来生成窗口着色报价。该页面包含下拉菜单,让用户选择manufacturer, model, type of film(front), type of film(side+rear)

下面是表单的代码

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

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

    <%= label :price_front, 'Front Tint' %>
    <div class="field">
        <%= collection_select(:price, :price_front, Price.order(:name), :id, :name, {:prompt => "Select Front Tint"}) %> 
    </div>

    <%= label :price_rear, 'Size and Back Tint' %>
    <div class="field">
        <%= collection_select(:price, :price_rear, Price.order(:name), :id, :name, {:prompt => "Select Side & Rear Tint"}) %> 
    </div>
    <div class="form-group">
        <%= submit_tag 'Submit' %>
    </div>
<% end %>

提交表单后,应将其重定向到/quotation/tints/generate 并显示下拉菜单中的值。但是,我收到一个错误,说Couldn't find Manufacturer with 'id'=。导致错误的代码如下所示

def generate
  @manufacturers = Manufacturer.find(params[:manufacturer_id])
end

这是调试日志中的参数

{"utf8"=>"✓",
 "authenticity_token"=>"Pl2bXiRT0AoF4i0h1RCHDbuvaKJNZOkV5ULQHKxDQgZzBWWLJ2mH7ddb9akwgxbloxBIHoVaT3pcwoIGcRufpg==",
 "tint"=>{"manufacturer_id"=>"7", "model_id"=>"6"},
 "price"=>{"price_front"=>"1", "price_rear"=>"2"},
 "commit"=>"Submit"}

我可以看到每个下拉值的id 在参数列表中正确显示。但是,我无法打印/quotation/tints/generate 的值,也无法获得制造商或型号的名称。

这里是routes.rb

get '/quotation/tints' => 'tints#quotation', :as => 'tints_quotation'
post '/quotation/tints/generate' => 'tints#generate', :as => 'generate_tints_quotation'

Tint.rb:

class Tint < ApplicationRecord
  has_many :manufacturers
  has_many :models
  belongs_to :manufacturer
  belongs_to :model

  validates_uniqueness_of :model_id, :scope => [:manufacturer_id]
end

Model.rb:

class Model < ApplicationRecord
  belongs_to :manufacturer, :dependent => :destroy
  validates :name, :presence => true
  validates_uniqueness_of :name, :scope => [:manufacturer_id]

  before_save :capitalize_content
end

Manufacruter.rb:

class Manufacturer < ApplicationRecord
  has_many :models, :dependent => :destroy
  validates :name, :presence => true, uniqueness: { case_sensitive: false }

  before_save :capitalize_content
end

tints.controller.rb:

def quotation
  render 'quotation'
end

def generate
  @manufacturers = Manufacturer.find(params[:manufacturer_id])
end

generate.html.erb:

<%= @manufacturers.name %>

我正在尝试打印所选的制造商

我尝试了多种方法来定义它,但我仍然面临同样的错误。任何帮助是极大的赞赏。

【问题讨论】:

  • 那些是您在 routes.rb 中唯一的路线吗?
  • 不,还有更多。 Rails.application.routes.draw do resources :prices resources :tints resources :models resources :manufacturers devise_for :users get '/quotation/tints' =&gt; 'tints#quotation', :as =&gt; 'tints_quotation' post '/quotation/tints/generate' =&gt; 'tints#generate', :as =&gt; 'generate_tints_quotation' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html root "pages#show", page: "index" end

标签: ruby-on-rails ruby ruby-on-rails-5


【解决方案1】:

在您的参数中,manufacturer_idtint 的嵌套值,而不是参数哈希的直接键。请尝试以下操作:

def generate
  @manufacturers = Manufacturer.find(params[:tint][:manufacturer_id])
end

【讨论】:

  • 您能否给我一些线索,如何打印front, side, and rear 表中的front, side, and rear 值?这就是我所做的,@manufacturers = Manufacturer.find(params[:tint][:manufacturer_id])@models = Model.find(params[:tint][:model_id]),我尝试使用 where 子句 @measurements = Tint.where('manufacturer_id' =&gt; @manufacturers).where('model_id' =&gt; @models) 过滤表。我试着做&lt;%= @measurements.front %&gt;,我收到了undefined method front' #<:activerecord_relation:0x000000102e7e48>`
  • 请通过发布新问题提出新问题。 Stack Overflow 使用问答格式,即一个问题,一个接受的答案。不幸的是,这种形式意味着涉及多个相关问题的滚动讨论并不适合。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 2015-07-15
  • 2018-02-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多