我的代码有什么问题? “1”的未定义方法“each”:字符串

What is wrong with my code? undefined method `each' for "1":String

你好,我正在创建一个食谱提交应用程序。在提交页面上,我有一个下拉列表来选择他们希望食谱所属的类别。当我点击提交时,我的食谱控制器出现错误:

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)

类别table:

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"}

您的迁移文件应该如下所示:

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

因为id是主键rails默认,不需要外部指定

同时修改strong params

更新

def recipe_params
  params.require(:recipe).permit(:nickname, :website, :instagram, :title, :description, :instructions, :ingredients, :notes, :embed, :photo, categories_attributes: [:id, :name])
end

collection_select 不适合 *_many 关系,而您的 Recipe 模型中有一个。要么将其更改为一对多关系,要么为此使用更好的输入,例如:

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

请注意 category_ids 而不是 categories 并相应地调整您的 recipe_params 方法。