Rails 4 未采用参数列表 - 控制台出错 - actionView::Template::error

Rails 4 not taking parameter list - error in console - actionView::Template::error

更新我在这里有两个问题 - 控制台中列出的那个与我发布的标题不匹配。 actionView::Template::error 的问题实际上是由于缺乏复数引起的 - 我在下面给出了最完整的答案 - 谢谢!

原题:

我正在使用 cocoon gemfile 并在没有 haml/slim 的情况下遵循它的 ERB 风格。我这里有我的 github - 我正在处理的 b运行ch 是 "cmodel" -Link。我自己解决了一个错误 - 但最后一个错误是难以捉摸的 - 也很奇怪 - 我丢失了通常显示成分和数量的字段 - 回到重点...

我的问题是...因为当我在 github 上搜索我的回购时 "ingredient_attributes" 并且仅在我的控制器参数中看到它...错误来自哪里以及如何修复它?

"Unpermitted parameter: ingredient_attributes"

"Unpermitted parameter: ingredients" 来自...我设法使它大部分工作 - 这是最后一个元素,我已经处理了大量文件并声明复数等,并尝试了 3 或 4 accept_parameters_for 的格式,然后是 ingredient_attributes.

的格式

当前错误的控制台输出:

Started GET "/recipes/1/edit" for 68.54.21.200 at 2015-12-21 02:53:27 +0000
Cannot render console from 68.54.21.200! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by RecipesController#edit as HTML
  Parameters: {"id"=>"1"}
  Recipe Load (0.2ms)  SELECT  "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "recipes".* FROM "recipes" WHERE "recipes"."id" = ? LIMIT 1  [["id", "1"]]
  Rendered recipes/_form.html.erb (8.2ms)
  Rendered recipes/edit.html.erb within layouts/application (9.8ms)
Completed 500 Internal Server Error in 16ms (ActiveRecord: 0.3ms)

ActionView::Template::Error (No association found for name `ingredients'. Has it been defined yet?):
    30: 
    31:   <fieldset id="recipe-ingredients">
    32:     <ol>
    33:       <%= f.fields_for :quantities do |quantity| %>
    34:         <%= render 'quantity_fields', f: quantity %>
    35:       <% end %>
    36:     </ol>
  app/models/quantity.rb:6:in `<class:Quantity>'
  app/models/quantity.rb:1:in `<top (required)>'
  app/views/recipes/_form.html.erb:33:in `block in _app_views_recipes__form_html_erb___1490943344300843366_36721460'
  app/views/recipes/_form.html.erb:1:in `_app_views_recipes__form_html_erb___1490943344300843366_36721460'
  app/views/recipes/edit.html.erb:3:in `_app_views_recipes_edit_html_erb___3608590124593016081_36957880'


  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (20.7ms)
  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.1ms)
  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.6ms)
  Rendered /usr/local/rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (99.4ms)

设置

型号:

class Recipe < ActiveRecord::Base
  has_many :quantities
  has_many :ingredient,
           :through => :quantities
  accepts_nested_attributes_for :quantities,
           :allow_destroy => true
  accepts_nested_attributes_for :ingredient
end

class Quantity < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
  accepts_nested_attributes_for :ingredient
end

class Ingredient < ActiveRecord::Base
  has_many :quantities
  has_many :recipes, through: :quantities
  accepts_nested_attributes_for :quantities
end

控制器:

class RecipesController < ApplicationController
  before_action :set_recipe, only: [:show, :edit, :update, :destroy]


  # GET /recipes/1/edit
  def edit
        @recipe = Recipe.find(params[:id])
  end

  # POST /recipes
  # POST /recipes.json
  def create
    @recipe = Recipe.new(recipe_params)

    respond_to do |format|
      if @recipe.save
        format.html { redirect_to @recipe, notice: 'Recipe was successfully created.' }
        format.json { render :show, status: :created, location: @recipe }
      else
        format.html { render :new }
        format.json { render json: @recipe.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /recipes/1
  # PATCH/PUT /recipes/1.json
  def update
    respond_to do |format|
      if @recipe.update(recipe_params)
        format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
        format.json { render :show, status: :ok, location: @recipe }
      else
        format.html { render :edit }
        format.json { render json: @recipe.errors, status: :unprocessable_entity }
      end
    end
  end


  private
    # Use callbacks to share common setup or constraints between actions.
    def set_recipe
      @recipe = Recipe.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def recipe_params
      #seems to work, but getting errors saving
      params.require(:recipe).permit(:title, :description, :instruction, 
      quantities_attributes:  [:id, :amount, :ingredient, :_destroy], 
      ingredient_attributes:  [:id, :name, :_destroy],
      recipe_attributes:      [:title, :description, :_destroy])
    end
end

这是我上面链接的 github 的观点...部分...

edit.html.erb

<h1>Editing Recipe</h1>

    <%= render 'form' %>

    <%= link_to 'Show', @recipe %> |
    <%= link_to 'Back', recipes_path %>

_form.html.erb

<%= form_for @recipe, html: {class: "form-horizontal"} 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 |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <fieldset id="recipe-meta">
    <ol>
      <li class="control-group">
        <%= f.label :title, "Recipe Name", class: "control-label" %>
        <div class="controls"><%= f.text_field :title %></div>
      </li>
      <li class="control-group">
        <%= f.label :description, "A brief description of this recipe", class: "control-label" %>
        <div class="controls"><%= f.text_area :description, rows: 5 %></div>
      </li>
      <li class="control-group">
        <%= f.label :instruction, "Instructions for this recipe", class: "control-label" %>
        <div class="controls"><%= f.text_area :instruction, rows: 10 %></div>
      </li>
    </ol>
  </fieldset>

  <fieldset id="recipe-ingredients">
    <ol>
      <%= f.fields_for :quantities do |quantity| %>
        <%= render 'quantity_fields', f: quantity %>
      <% end %>
    </ol>
    <%= link_to_add_association 'add ingredient', f, :quantities, 'data-association-insertion-node' => "#recipe-ingredients ol", 'data-association-insertion-method' => "append", :wrap_object => Proc.new {|quantity| quantity.build_ingredient; quantity } %>
  </fieldset>
    <%= f.submit %>
  </div>
<% end %>

_quantity_fields.html.erb

<li class="control-group nested-fields">
  <div class="controls">
    <%= f.label :amount, "Amount:" %>
    <%= f.text_field :amount %>

    <%= f.fields_for :ingredient do |quantity_ingredient| %>
      <%= quantity_ingredient.text_field :name %>
    <% end %>

    <%= link_to_remove_association "remove", f %>
  </div>
</li>

声明关联 class 配方has_many 关系时出错。 尝试在 配方模型

中更改与以下内容的关系
has_many :ingredients

作为 Praveen George 答案的补充,如果您按照屏幕上的说明(大部分时间)进行操作,则可以相对简单地调试 Rails:

No association found for name `ingredients'. Has it been defined yet?

这基本上意味着您没有在您调用的模型中定义 ingredients 关联。

正如Praveen所指出的,您的问题出在Recipe模型上。您必须以复数名称来称呼该协会:

#app/models/recipe.rb
class Recipe < ActiveRecord::Base
   has_many :ingredients
   accepts_nested_attributes_for :ingredients
end