0

我是 Rails 4 上的 Rails 初学者。我正在尝试制作酒单,并拥有一个具有三个属性的酒模型,:name、:year 和:type。对于 :type 我创建了一个选择列表,其中包括不同品种的葡萄酒。每当我尝试创造一种新葡萄酒时,我都会遇到例外Invalid single-table inheritance type: Merlot is not a subclass of Wine(我从我的选择列表中选择了“梅洛”)。我不确定我做错了什么。这是我来自 schema.rb 的葡萄酒模式。

create_table "wines", force: true do |t|
    t.integer  "year"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
    t.string   "type"
  end

这是我的新酒单..

<%= form_for @wine do |f| %>
    <%= render "layouts/errors", object: @wine %>
    <div class="form-group input-group input-group-lg">
        <span class="input-group-addon"><i class="glyphicon glyphicon-glass"></i></span>
        <%= f.text_field :name, placeholder: "What's the wine name or vinyard?", class: "form-control input-lg" %>
    </div>
    <div class="form-group">
        <%= f.label :year, "Select year" %>
        <%= select_year Date.today, {start_year: Time.now.year, end_year: Time.now.year - 90, field_name: :year, prefix: :wine}, {class: "form-control"} %>
    </div>
    <div class="form-group">
        <%= f.label :type, "Select type" %>
        <%= f.select :type, options_for_select(Wine::TYPES), { include_blank: false }, { class: "form-control" } %>

    </div>
    <div class="form_group">
        <%= f.submit "Add it", class: "btn btn-success btn-block" %>
    </div>
<% end %>

我的葡萄酒模型..

class Wine < ActiveRecord::Base
    validates :name, presence: true, uniqueness: { case_sensitive: false }
    TYPES = [
              ["Cabernet Sauvignon"],
              ["Chardonnay"],
              ["Zinfandel"],
              ["Merlot"],
              ["Sauvignon Blanc"],
              ["Syrah/Shiraz"],
              ["Pinot Gris/Grigio"],
              ["Malbec"],
              ["Petite Sirah"],
              ["Pinot Noir"],
              ["Riesling"],
              ["Champagne/Sparkling Wine"],
              ["White Zinfandel"],
              ["Blend"],
              ["Other"]
            ]
end

和我的 wines_controller.rb

class WinesController < ApplicationController
  before_action :set_wine, only: [:show, :edit, :update, :destroy]

  def index
    @wines = Wine.all
  end

  def new
    @wine = Wine.new

  end

  def create
    @wine = Wine.new(wine_params)
    if @wine.save
        flash[:notice] = "Successfully created..."
        redirect_to @wine
    else
        flash.now[:error] = "There was a problem"   
        render "new"
    end
  end

  def show
  end

  def edit
  end

  def update
    if @wine.update(wine_params)
        redirect_to @wine
    else
        flash[:error] = "Something went wrong"
        render "edit"
    end
  end

  def destroy
    @wine.destroy
    redirect_to wines_path
  end

  private

    def set_wine
        @wine = Wine.find(params[:id])
    end

    def wine_params
        params.require(:wine).permit(:name, :year, :type)
    end
end

谁能看到我做错了什么?谢谢。

4

1 回答 1

2

type字段用于创建 STI(单表继承)。尝试重命名字段或关闭 STI:

class Wine < ActiveRecord::Base
  ...
  self.inheritance_column = nil
  ...
于 2013-09-06T16:08:27.923 回答