如何在rails 4中的form_for中正确创建选择列表?

时间:2013-09-06 15:51:01

标签: ruby-on-rails

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

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

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

1 个答案:

答案 0 :(得分:2)

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

class Wine < ActiveRecord::Base
  ...
  self.inheritance_column = nil
  ...