未初始化的常量Recipe :: CategoryId

时间:2015-07-27 17:28:08

标签: ruby-on-rails ruby forms

我试图为每个食谱添加类别。用户通过表单选择类别。我得到一个未初始化的常量Recipe :: CategoryId错误,有人知道whatsup吗?

食谱模型:

class Recipe < ActiveRecord::Base
   validates :description, :nickname, :title, :ingredients, :instructions, :category_ids, presence: true

   has_many :category_ids
end

类别模型:

class Category < ActiveRecord::Base
  belongs_to :recipes
end
食谱控制器:

class RecipesController < ApplicationController
  def index
    @recipes = Recipe.all
  end

  def show
    @recipe = Recipe.find(params[:id])
  end

  def new
    @recipe = Recipe.new

  end

  def edit
    @recipe = Recipe.find(params[:id])
  end

  def create
    @recipe = Recipe.new(recipe_params)

    if @recipe.save
      redirect_to recipes_url
    else
      render:new
  end
end

def update
    @recipe = Recipe.find(params[:id])

    if @recipe.update_attributes(recipe_params)
      redirect_to recipe_path(@recipe)
    else
      render :edit
    end
  end

  def destroy
    @recipe = Recipe.find(params[:id])
    @recipe.destroy
    redirect_to recipes_path
  end

  private
  def recipe_params
    params.require(:recipe).permit(:nickname, :website, :instagram, :title, :description, :instructions, :ingredients, :notes, :embed, :photo, :category_ids)
  end

end

形式:

<%= form_for(@recipe) do |f| %>

 <% if @recipe.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@recipe.errors.count, "error") %> prohibited this recipe from being saved:</h2>

      <ul>
      <% @recipe.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>


  <div class="field">
    <%= f.label :nickname %><br />
    <%= f.text_field :nickname %>
  </div>
  <div class="field">
    <%= f.label :website %><br />
    <%= f.text_area :website %>
  </div>
  <div class="field">
    <%= f.label :instagram %><br />
    <%= f.text_area :instagram %>
  </div>
  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_area :title %>
  </div>
  <div class="field">
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </div>
  <div class="field">
    <%= f.label :ingredients %><br />
    <%= f.text_area :ingredients %>
  </div>
  <div class="field">
    <%= f.label :instructions %><br />
    <%= f.text_area :instructions %>
  </div>
  <div class="field">
    <%= f.label :notes %><br />
    <%= f.text_area :notes %>
  </div>
  <div class="field">
    <%= f.label :embed %><br />
    <%= f.text_area :embed %>
  </div>
   <div class="field">
    <%= f.label :photo %><br />
    <%= f.text_area :photo %>
  </div>
  <div class="field">

<%= f.collection_check_boxes :category_ids, Category.all, :id, :name %>

</div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

食谱迁移:

class CreateRecipes < ActiveRecord::Migration
  def change
    create_table :recipes do |t|
    t.string   :nickname
    t.string   :website
    t.string   :instagram
    t.string   :title
    t.string   :description
    t.string   :ingredients
    t.string   :instructions
    t.string   :notes
    t.string   :embed
    t.string   :photo

      t.timestamps null: false
    end
  end
end

类:

class CreateCategories < ActiveRecord::Migration
 def change
    create_table :categories do |t|
      t.string :name
      t.timestamps
    end
  end
end

模式:

ActiveRecord::Schema.define(version: 20150722174136) do

  create_table "categories", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end



  create_table "recipes", force: :cascade do |t|
    t.string   "nickname"
    t.string   "website"
    t.string   "instagram"
    t.string   "title"
    t.string   "description"
    t.string   "ingredients"
    t.string   "instructions"
    t.string   "notes"
    t.string   "embed"
    t.string   "photo"
    t.datetime "created_at",   null: false
    t.datetime "updated_at",   null: false
  end

end

1 个答案:

答案 0 :(得分:1)

假设您希望食谱和类别之间存在多对多关系,请首先创建多对多虚拟联接表,如下所示:

create_table "categories_recipes" do |t|
    t.integer   "recipe_id"
    t.integer   "category_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
end

然后添加关联。

class Recipe < ActiveRecord::Base
   validates :description, :nickname, :title, :ingredients, :instructions, :category_ids, presence: true

   has_and_belongs_to_many :categories
end

您还需要将其添加到类别模型中

class Category < ActiveRecord::Base
  #other stuffs
  has_and_belongs_to_many :recipes
end