【问题标题】:uninitialized constant Recipe::CategoryId未初始化的常量 Recipe::CategoryId
【发布时间】:2015-07-27 17:28:08
【问题描述】:

我正在尝试为每个食谱添加类别。用户通过表格选择类别。我收到一个未初始化的常量 Recipe::CategoryId 错误,有人知道怎么回事吗?

配方模型:

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

【问题讨论】:

  • 这个 has_many :category_ids 应该是 has_many :categories 验证也应该在类别而不是 category_ids 上
  • 还有belongs_to :recipes 应该是belongs_to :recipe
  • 谢谢大家!除了现在我收到此错误:错误禁止保存此配方:类别不能为空白...如果我选择它为什么它说它是空白的?

标签: ruby-on-rails ruby forms


【解决方案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

【讨论】:

  • 谢谢!现在我得到这个错误:找不到表'categories_recipes'......我应该创建表吗?如果可以,我应该在里面放什么?
猜你喜欢
  • 2015-04-14
  • 1970-01-01
  • 2011-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多