大家好我正在创建一个食谱提交应用程序。在提交页面上,我有一个下拉列表,用于选择他们希望配方所在的类别。当我点击提交时,我在Recipes控制器中收到错误:
undefined method `each' for "1":String
突出显示错误:
def create
@recipe = Recipe.new(recipe_params)
这里发生了什么?
我的下拉类别:
<%= f.collection_select :categories, Category.all, :id, :name, :prompt => "Select a category" %>
分类模型:
class Category < ActiveRecord::Base
has_and_belongs_to_many :recipes
end
食谱模型:
class Recipe < ActiveRecord::Base
validates :description, :nickname, :title, :ingredients, :instructions, :categories, presence: true
has_and_belongs_to_many :categories
end
种子:(我为每个类别分配一个ID,因此“1”是我在下拉列表中选择的ID。)
Category.delete_all
Category.create(:name => "Vegan",
:id => 1)
Category.create(:name => "Low Carb",
:id => 2)
Category.create(:name => "Ketogenic",
:id => 3)
Category.create(:name => "Paleo",
:id => 4)
Category.create(:name => "Flour-free",
:id => 5)
Category.create(:name => "Misc",
:id => 6)
类别表:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories, :primary_key => :id do |t|
t.string :name
t.timestamps null: false
end
end
change_column :categories, :id, :string
end
分类控制器:
class CategoriesController < ApplicationController
class CategoriesController < ApplicationController
def index
@categories = Category.all
end
end
def show
@category = Category.find(params[:id])
end
end
def new
@category = Category.new
end
end
def edit
@category = Category.find(params[:id])
end
def create
@category = Category.new(params[:category])
end
def update
@category = Category.find(params[:id])
respond_to do |format|
if @category.update_attributes(params[:category])
format.html { redirect_to @category, notice: 'Category was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
def destroy
@category = Category.find(params[:id])
@category.destroy
respond_to do |format|
format.html { redirect_to categories_url }
format.json { head :no_content }
end
end
end
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, :categories)
end
end
错误日志:
undefined method `each' for "1":String
Extracted source (around line #19):
17
18
19
20
21
22
def create
@recipe = Recipe.new(recipe_params)
if @recipe.save
redirect_to recipes_url
Rails.root: /Users/ashleighalmeida/mixi
Application Trace | Framework Trace | Full Trace
app/controllers/recipes_controller.rb:19:in `create'
Request
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"l01VMCMWBhW4MuWINPqolGwqrM5AIJScw/4+FjupVMLRWKX0okdVYCSME+BJD97XjklWYLhKdflBTRPcucXm3w==",
"recipe"=>{"nickname"=>"sf",
"website"=>"sdfs",
"instagram"=>"sdfs",
"title"=>"sdfs",
"description"=>"sdfs",
"ingredients"=>"sdfs",
"instructions"=>"sdfs",
"notes"=>"sdfs",
"embed"=>"sdfs",
"photo"=>"sfds",
"categories"=>"1"},
"commit"=>"Create Recipe"}
答案 0 :(得分:1)
您的迁移文件应如下所示:
class CreateCategories < ActiveRecord::Migration
def change
create_table :categories do |t|
t.string :name
t.timestamps
end
end
end
由于id
是主键,因此无需指定外部
并修改strong params
<强>更新强>
def recipe_params
params.require(:recipe).permit(:nickname, :website, :instagram, :title, :description, :instructions, :ingredients, :notes, :embed, :photo, categories_attributes: [:id, :name])
end
答案 1 :(得分:0)
collection_select
不适合* _many关系,并且您的Recipe
模型中有一个关系。要么将其更改为一对多关系,要么使用更好的输入,例如:
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name %>
注意category_ids
而不是categories
并相应地调整recipe_params
方法。