【发布时间】: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