oobie11
oobie11

Reputation: 925

How do I correctly make a select list in a form_for in rails 4?

I'm a rails beginner on rails 4. I am trying to make a wine list and have a wine model that has three attributes, :name, :year, and :type. For the :type i have created a select list of options that include different varietals of wine. When ever I try and create a new wine, I get the exception Invalid single-table inheritance type: Merlot is not a subclass of Wine (I chose "Merlot" from my select list). Im not sure what I'm doing wrong. Here is my wine schema from 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

And here is my new wine form ..

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

my wine model ..

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

and my 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

Can anyone see what I'm doing wrong? Thanks.

Upvotes: 0

Views: 295

Answers (1)

dimuch
dimuch

Reputation: 12818

type field is used to create STI (Single Table Inheritance). Try to rename the field or turn STI off:

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

Upvotes: 2

Related Questions