创建新记录时出现错误,我遇到了问题:
NoMethodError in CoffeeRoastsController#create
undefined method `name' for #<CoffeeBlend:0x007f95a0d168d8>
我正在尝试创建一个具有has_many coffee_roast
的新coffee_blends
,这是一个也具有has_many coffee_beans
的联接表。我的coffee_roast/_form.html.erb
拥有一个精选集合,可以浏览各种coffee_beans
_form
<%= form_with(model: coffee_roast, local: true) do |form| %>
<% if coffee_roast.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(coffee_roast.errors.count, "error") %> prohibited this coffee_roast from being saved:</h2>
<ul>
<% coffee_roast.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-6">
<div class="field form-group">
<%= form.label :roaster, class: 'control-label' %><br />
<%= form.collection_select(:roaster_id, Roaster.order(:roaster_name), :id, :roaster_name, :prompt => 'Choose Roaster') %>
</div>
<div class="field">
<strong><%= form.label :name %></strong>
<%= form.text_field :name, id: :coffee_roast_name %>
</div>
<div class="field form-group">
<%= collection_check_boxes :coffee_roast, :coffee_bean_ids, CoffeeBean.all.order(name: :asc), :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text } %>
<% end %>
</div>
<div class="field form-group">
<%= collection_check_boxes :coffee_roast, :flavour_ids, Flavour.all.order(name: :asc), :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text } %>
<% end %>
</div>
<div class="form-group">
<%= form.label :style, "Style", class: 'control-label' %><br />
<%= form.select :style, [ 'Espresso','Filter' ], :prompt => 'Select One', id: :coffee_style, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :strength, "Strength", class: 'control-label' %><br />
<%= form.select :strength, [ 'Light','Medium','Dark' ], :prompt => 'Select One', id: :coffee_strength, class: "form-control" %>
</div>
<div class="form-group">
<%= form.label :image %>
<%= form.file_field :image, id: :coffee_roast_image %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
如果不选择任何bean,我就可以创建记录。
由于错误表明name
是CoffeeBlend的未定义方法,我怀疑该collection_select中存在潜在的错误,但我无法弄清楚它会是什么。
我能够创建新的coffee_beans,因此可以很好地更新coffee_blend joins表。
模型
class CoffeeBlend < ApplicationRecord
belongs_to :coffee_bean
belongs_to :coffee_roast
class CoffeeBean < ApplicationRecord
has_many :coffee_blends
has_many :coffee_roasts, through: :coffee_blends
belongs_to :country
class CoffeeRoast < ApplicationRecord
belongs_to :roaster
has_many :coffee_blends
has_many :coffee_beans, through: :coffee_blends
has_one_attached :image
控制器
def create
@coffee_roast = CoffeeRoast.new(coffee_roast_params)
respond_to do |format|
if @coffee_roast.save
format.html { redirect_to @coffee_roast, notice: 'Coffee roast was successfully created.' }
format.json { render :show, status: :created, location: @coffee_roast }
else
format.html { render :new }
format.json { render json: @coffee_roast.errors, status: :unprocessable_entity }
end
end
end
控制台
Started POST "/coffee_roasts" for 127.0.0.1 at 2019-01-04 08:17:05 +0000
Processing by CoffeeRoastsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"+MbuyujdR1gSTRaylqeUjto4ZqhgnNnGfnPRiNEevdu2wePduXtEugbtmNpx6HGrtivFWZC92KGwTGJSy9+A2A==", "coffee_roast"=>{"roaster_id"=>"6", "name"=>"Revelation", "coffee_bean_ids"=>["", "4", "5"], "flavour_ids"=>["", "3", "2"], "style"=>"Espresso", "strength"=>"Dark"}, "commit"=>"Create Coffee roast"}
CoffeeBean Load (44.8ms) SELECT "coffee_beans".* FROM "coffee_beans" WHERE "coffee_beans"."id" IN ($1, $2) [["id", 4], ["id", 5]]
Flavour Load (0.9ms) SELECT "flavours".* FROM "flavours" WHERE "flavours"."id" IN ($1, $2) [["id", 3], ["id", 2]]
(1.0ms) BEGIN
CoffeeRoast Exists (43.7ms) SELECT 1 AS one FROM "coffee_roasts" WHERE "coffee_roasts"."id" IS NOT NULL AND "coffee_roasts"."slug" = $1 LIMIT $2 [["slug", "revelation"], ["LIMIT", 1]]
Roaster Load (0.8ms) SELECT "roasters".* FROM "roasters" WHERE "roasters"."roaster_id" = $1 LIMIT $2 [["roaster_id", 6], ["LIMIT", 1]]
(0.6ms) ROLLBACK
Completed 500 Internal Server Error in 280ms (ActiveRecord: 116.2ms)
NoMethodError (undefined method `name' for #<CoffeeBlend:0x007f95a3ad10e8>):
答案 0 :(得分:0)
感谢@vasilisa,她发现我试图在slug
联接表中创建一个coffee_blend
,其中没有名为:name
的字段