【问题标题】:Rails 4 Nested Attributes with simple_fields_for One Saves and One Doesn't Save to DatabaseRails 4 嵌套属性与 simple_fields_for 一个保存和一个不保存到数据库
【发布时间】:2016-02-13 09:38:22
【问题描述】:

研究:[Rails 4 带有fields_for 的嵌套属性不保存到数据库] [Rails 4 - 带有Cocoon gem 的嵌套属性]1[Rails 4.1 用于获取未经许可的参数和不保存的嵌套属性和字段]2

我的具体问题是:

我有两种嵌套形式:成分和方向

成分保存,但说明不保存。它不会抛出错误,日志也不知道是否存在问题。

首先,让我们解决最常见的问题:为强参数错误地命名属性参数。我的是正确的复数。

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 年 2 月 12 日 19:00:07 -0800 开始为 ::1 发布“/recipes” RecipesController#create 作为 HTML 处理 参数:{"utf8"=>"✓", "authenticity_token"=>"YEXiv10mHkfdLPRFGHFGNJX2szJQVXK7gezeakEFbe+57afx6Ih1UjRS6tJNftDLsMI5NS1W84pf2sRhQi0J8g==", "recipe"=>{"title"=>"蜂蜜苹果鸡", "image"=>#, 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">, "ingredients_attributes"=>{"1455332308170"=>{"name"=>"鸡大腿", "_destroy"=>"false"}, "1455332308175"=>{"name"=>"Honey", "_destroy"=>"false"}}, "directions_attributes"=>{"1455332325877"= >{"step"=>"带皮的棕色鸡。", "_destroy"=>"false"}, "1455332325880"=>{"step"=>"添加苹果", "_destroy"=>"false" }}},“提交”=>“创建配方”} 用户负载 (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 不允许的参数:step 不允许的参数:step (0.1ms) 开始交易 配方存在(0.2ms)从“食谱”中选择 1 作为一个,其中“食谱”。“图像”=?限制 1 [[“图像”,“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", "蜂蜜苹果鸡"], ["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) 提交事务 重定向到http://localhost:3000/recipes/1 完成 302 Found in 1499ms (ActiveRecord: 3.5ms)

于 2016 年 2 月 12 日 19:00:09 -0800 开始为 ::1 获取“/recipes/1” 由 RecipesController#show 处理为 HTML 参数:{"id"=>"1"} 配方加载 (0.3ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."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) SELECT "directions".* FROM "directions" WHERE "directions"."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) 在 47 毫秒内完成 200 次 OK(查看次数:41.2 毫秒 | ActiveRecord:1.0 毫秒)

我不明白 Karl 对他自己问题的回答:Rails 4.1 Nested Attributes and Fields For Getting Unpermitted Parameters and Not Saving 如果你能看到任何东西,你就是 Macgyver!

【问题讨论】:

  • 卡尔的回答不相关:他自己在操纵 javascript/html。 @elias-sanchez 的回答对我来说似乎是正确的:你不允许在你的强参数中使用 step,这很可能会导致模型被拒绝(然后它会失败而没有错误)。但是如果没有任何食谱的模型代码,很难确定。

标签: ruby-on-rails-4 nested-forms cocoon-gem


【解决方案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 列的 not_blank 验证,它将不会被保存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多