代码之家  ›  专栏  ›  技术社区  ›  Simon Cooper

无法在Rails 5.2中创建记录

  •  0
  • Simon Cooper  · 技术社区  · 6 年前

    我在创建新记录时遇到问题,获取错误:

    NoMethodError in CoffeeRoastsController#create
    undefined method `name' for #<CoffeeBlend:0x007f95a0d168d8>
    

    coffee_roast 有很多 coffee_blends 这是一个连接表,它也有 coffee_beans . 我的 coffee_roast/_form.html.erb 有一个精选的集合 咖啡豆

    _形式

    <%= 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

    我可以创建新的咖啡豆,因此更新咖啡混合加入表罚款。

    模型

    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>):
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Simon Cooper    6 年前

    多亏了瓦西里斯,她发现我想创造一个 slug coffee_blend 联接表中没有调用的字段 :name