【发布时间】: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' => 'tints#quotation', :as => 'tints_quotation' post '/quotation/tints/generate' => 'tints#generate', :as => '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