研究:[Rails 4嵌套属性,其中fields_for不保存到数据库] [Rails 4 - 使用Cocoon gem的嵌套属性] 1 [Rails 4.1嵌套属性和字段,用于获取未允许的参数但不保存] { {3}}
我的具体问题是:
我有两种嵌套形式:成分和方向
成分保存,但方向不保存。它不会引发错误,日志也不知道是否存在问题。
首先,让我们解决最常见的问题:强参数的错误命名属性参数。我是正确的复数。
class RecipesController < ApplicationController
def recipe_params
params.require(:recipe).permit(:title, :image, directions_attributes: [:id, :name, :_destroy], ingredients_attributes: [:id, :name, :_destroy])
end
我的模型也正确设置
class Recipe < ActiveRecord::Base
has_many :ingredients
has_many :directions
belongs_to :user
accepts_nested_attributes_for :ingredients, reject_if: :all_blank, allow_destroy: true
accepts_nested_attributes_for :directions, reject_if: :all_blank, allow_destroy: true
validates :title, presence: true
mount_uploader :image, ImageUploader
end
#_form.html.haml
= simple_form_for @recipe, html: { multipart: true } do |f|
- if @recipe.errors.any?
#errors
%p
= @recipe.error.count
Prevented this recipe from saving
%ul
%li= msg
.panel-body
= f.input :title, label: "Name", input_html: { class: 'form-control' }
= f.input :image, label: "Picture",input_html: { class: 'form-control' }
.row
.col-md-6
%h3 Ingredients
#ingredients
= f.simple_fields_for :ingredients do |ingredient|
= render 'ingredient_fields', f: ingredient
.links
= link_to_add_association 'Add Ingredient', f, :ingredients, class: "btn btn-default add-button"
.col-md-6
%h3 Directions
#directions
= f.simple_fields_for :directions do |direction|
= render 'direction_fields', f: direction
.links
= link_to_add_association 'Add Step', f, :directions, class: "btn btn-default add-button"
= f.button :submit, class: "btn btn-primary"
以下是我对成分和方向的偏爱
_ingredient_fields.html.haml
.form-inline.clearfix
.nested-fields
= f.input :name, input_html: { class: "form-input form-control"}
= link_to_remove_association 'Remove', f, class: "form-button btn btn-default"
_direction_fields.html.haml
.form-inline.clearfix
.nested-fields
= f.input :name, input_html: { class: "form-input form-control"}
= link_to_remove_association 'Remove', f, class: "form-button btn btn-default"
好的,这是有趣的事情,我的应用程序不会抛出任何错误。名称,图像和成分保存,但方向不。
这是日志
在2016-02-12 19:00:07 -0800为:: 1开始POST“/ recipes” RecipesController处理#create as HTML 参数:{“utf8”=&gt;“✓”,“authenticity_token”=&gt;“YEXiv10mHkfdLPRFGHFGNJX2szJQVXK7gezeakEFbe + 57afx6Ih1UjRS6tJNftDLsMI5NS1W84pf2sRhQi0J8g ==”,“recipe”=&gt; {“title”=&gt;“Honey Apple Chicken”,“image”= &gt;#,@ original_filename =“Honey-Mustard-Chicken-and-Apples.jpg”,@ content_type =“image / jpeg”,@ headers =“Content-Disposition:form-data; name = \”recipe [image] \“; filename = \”Honey-Mustard-Chicken-and-Apples.jpg \“\ r \ nContent-Type:image / jpeg \ r \ n”&gt;,“ingredients_attributes”=&gt; {“1455332308170”=&gt; ; {“name”=&gt;“Chicken Thighs”,“_ destroy”=&gt;“false”},“1455332308175”=&gt; {“name”=&gt;“Honey”,“_ destroy”=&gt;“false” },“directions_attributes”=&gt; {“1455332325877”=&gt; {“step”=&gt;“Brown Chicken with skin on。”,“_ destroy”=&gt;“false”},“1455332325880”=&gt; { “step”=&gt;“添加苹果”,“_ destroy”=&gt;“false”}}},“提交”=&gt;“创建食谱”} 用户负载(0.1ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=? ORDER BY“users”。“id”ASC LIMIT 1 [[“id”,1]] 未经许可的参数:步骤 未经许可的参数:步骤 (0.1ms)开始交易 食谱存在(0.2ms)SELECT 1 AS一个来自“食谱”WHERE“食谱”。“image”=? LIMIT 1 [[“image”,“1455332407-84040-0233 / Honey-Mustard-Chicken-and-Apples.jpg”]] SQL(0.4ms)INSERT INTO“recipes”(“title”,“image”,“user_id”,“created_at”,“updated_at”)VALUES(?,?,?,?,?)[[“title”,“ Honey Apple Chicken“],[”image“,”1455332407-84040-0233 / Honey-Mustard-Chicken-and-Apples.jpg“],[”user_id“,1],[”created_at“,”2016-02- 13 03:00:07.756946“],[”updated_at“,”2016-02-13 03:00:07.756946“]] SQL(0.2ms)INSERT INTO“ingredients”(“name”,“recipe_id”,“created_at”,“updated_at”)VALUES(?,?,?,?)[[“name”,“Chicken Thighs”],[ “recipe_id”,1],[“created_at”,“2016-02-13 03:00:07.758440”],[“updated_at”,“2016-02-13 03:00:07.758440”]] SQL(0.1ms)INSERT INTO“ingredients”(“name”,“recipe_id”,“created_at”,“updated_at”)VALUES(?,?,?,?)[[“name”,“Honey”],[“ recipe_id“,1],[”created_at“,”2016-02-13 03:00:07.759655“],[”updated_at“,”2016-02-13 03:00:07.759655“]] (2.5ms)提交事务 重定向到2 完成302发现于1499ms(ActiveRecord:3.5ms)
在2016-02-12 19:00:09 -0800为:: 1开始获取“/ recipes / 1” RecipesController处理#show as HTML 参数:{“id”=&gt;“1”} 配方加载(0.3ms)SELECT“食谱”。* FROM“食谱”WHERE“食谱”。“id”=?限制1 [[“id”,1]] 用户负载(0.2ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=?限制1 [[“id”,1]] 成分负荷(0.3ms)选择“成分”。*来自“成分”WHERE“成分”。“recipe_id”=? [[“recipe_id”,1]] 方向载荷(0.1ms)选择“方向”。* FROM“方向”WHERE“方向”。“recipe_id”=? [[“recipe_id”,1]] 用户负载(0.1ms)SELECT“users”。* FROM“users”WHERE“users”。“id”=? ORDER BY“users”。“id”ASC LIMIT 1 [[“id”,1]] 在布局/应用程序中呈现recipes / show.html.haml(9.2ms) 在47ms完成200 OK(浏览次数:41.2ms | ActiveRecord:1.0ms)
我不理解卡尔对自己问题的回答:http://localhost:3000/recipes/1 如果你能看到任何东西,那你就是Macgyver!
答案 0 :(得分:1)
您可以更改:
params.require(:recipe).permit(:title, :image, directions_attributes: [:id, :name, :_destroy], ingredients_attributes: [:id, :name, :_destroy])
的
params.require(:recipe).permit(:title, :image, directions_attributes: [:id, :name, :step, :_destroy], ingredients_attributes: [:id, :name, :_destroy])
如果不允许,您尝试发送“步骤”,如果您在针对setp列的Direction模型中进行了not_blank验证,则不会保存它。