首页 文章

Rails 4嵌套属性,simple_fields_for一个保存,一个不保存到数据库

提问于
浏览
0

研究:[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"

好的,这是有趣的事情,我的应用程序不会抛出任何错误 . 名称,图像和成分保存,但方向不 .

这是日志

开始POST "/recipes"作为:: 1在2016年2月12日19时00分07秒-0800处理通过的RecipesController#创建以HTML参数:{"utf8" => "✓","authenticity_token" => "YEXiv10mHkfdLPRFGHFGNJX2szJQVXK7gezeakEFbe+57afx6Ih1UjRS6tJNftDLsMI5NS1W84pf2sRhQi0J8g==","recipe" => {"title" => "Honey Apple Chicken","image" =>#,@ original_filename = "Honey-Mustard-Chicken-and-Apples.jpg",@ CONTENT_TYPE = "image/jpeg",@头= "Content-Disposition: form-data; name="食谱[图像] \ "; filename="蜂蜜 - 芥菜 - 鸡和Apples.jpg \ "\r\nContent-Type: image/jpeg\r\n">,"ingredients_attributes" => {"1455332308170" => {"name" => "Chicken Thighs","_destroy" => "false"},"1455332308175" => {"name" => "Honey","_destroy" => "false"}},"directions_attributes" => {"1455332325877" => {"step" => "Brown Chicken with skin on.","_destroy" => "false"},"1455332325880" => {"step" => "Add apples","_destroy" => "false"}}},"commit" => "Create Recipe"}用户负载(0.1毫秒)SELECT "users" . * FROM "users" WHERE "users" . "id" =? ORDER BY "users" . "id" ASC LIMIT 1 [["id",1]] Unpermitted parameter: step Unpermitted parameter: step (0.1ms)开始事务配方存在(0.2ms)SELECT 1 AS one FROM "recipes" WHERE "recipes" . "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)提交事务重定向到http://localhost:3000/recipes/1完成302在1499ms实测值(ActiveRecord的:3.5ms)

在2016-02-12 19:00:09 -0800开始GET“/ recipes / 1”for :: 1由RecipesController处理#show as HTML参数:{“id”=>“1”}配方负载(0.3ms) SELECT“食谱” . * FROM“食谱”WHERE“食谱” . “id”=? LIMIT 1 [[“id”,1]]用户负载(0.2ms)SELECT“users” . * FROM“users”WHERE“users” . “id”=? LIMIT 1 [[“id”,1]]成分负荷(0.3ms)选择“成分” . *来自“成分”WHERE“成分” . “recipe_id”=? [[“recipe_id”,1]]方向加载(0.1ms)选择“方向” . * 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]]在布局/应用程序(9.2ms)内渲染食谱/ show.html.haml在47ms完成200 OK(浏览次数:41.2ms | ActiveRecord: 1.0ms的)

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

1 回答

  • 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验证,则不会保存它 .

相关问题