【问题标题】:What is wrong with my code? undefined method `each' for "1":String我的代码有什么问题? “1”的未定义方法“每个”:字符串
【发布时间】:2015-10-12 19:07:55
【问题描述】:

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

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

【问题讨论】:

  • 粘贴完整的错误日志
  • 使用下拉菜单,您只能选择一个类别,那么has_and_belongs_to_many 关系是否有意义?另外,RecipesController 中可能出现错误,我们需要它。
  • 为什么要将主键字段更改为字符串?
  • recipe_params 长什么样子?
  • @lurker params.require(:recipe).permit(:nickname, :website, :instagram, :title, :description, :instructions, :ingredients, :notes, :embed, :photo, :categories)

标签: ruby-on-rails forms schema categories


【解决方案1】:

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

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

【讨论】:

  • 非常感谢您的宝贵时间。我做了改变。你是最棒的!除了现在我在点击提交时收到此错误:1 错误禁止保存此食谱:类别不能为空白.. 如果我从下拉列表中选择它,为什么它说它是空白的?
  • @icecreamrabbit 更新了帖子,请看一下,谢谢
  • 再次感谢您的光临。我进行了更改,现在我得到了:i60.tinypic.com/3340psl.png :( 叹息我不知道“每个”来自哪里
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多