Обновление: У меня здесь две проблемы: та, что указана в консоли, не соответствует опубликованному мною заголовку. Проблема с actionView::Template::error на самом деле была вызвана отсутствием множественного числа - я дал ответ на самый полный ответ ниже - спасибо!
Исходный вопрос:
Я работаю с коконом gemfile и следую стилю ERB без haml/slim. У меня есть мой github здесь. Ветка, над которой я работаю, называется «cmodel» — Link. Я позаботился об одной из ошибок, но эта последняя просто неуловима - тоже странно - я потерял поле, которое обычно показывает ингредиент с количеством - вернемся к делу...
Мой вопрос... так как, когда я ищу в своем репозитории на github "ingredient_attributes" и вижу его только в параметрах моего контроллера... откуда возникает ошибка и что ее исправить?
"Недопустимый параметр: ингридиент_атрибуты"
or
«Недопустимый параметр: ингредиенты» исходит из ... Мне удалось заставить это в основном работать - это последний элемент, и я просмотрел кучу файлов и указал множественное число и т. Д., Наряду с попытками 3 или 4 форматов для accept_parameters_for а затем ингридиент_атрибуты.
Мой консольный вывод для текущей ошибки:
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)
Настройка
- Я запустил установку пакета для gemfile.
- Я сделал запись в Assets/application.js для «//= требуется кокон». Он был скомпилирован и разграблен.
- Я настраиваю контроллер для прямого просмотра модели ингредиентов и могу добавлять их вручную через страницу ингредиентов, но он никогда не сохраняет мой ингредиент через модель рецепта.
Модели:
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>
github
? 21.12.2015