2016-02-13 82 views
0

研究:[Rails 4嵌套屬性與fields_for不保存到數據庫] [Rails 4 - 與Cocoon寶石的嵌套屬性] 1 [Rails 4.1嵌套屬性和字段獲取未經許可的參數和不保存] 2Rails 4嵌套屬性與simple_fields_for一個保存和一個不保存到數據庫

我的具體問題是:

我有兩個嵌套形式:主料和路線

成分保存,但方向不保存。它不會引發錯誤,日誌也不會知道是否存在問題。

首先,讓我們避開最常見的問題:爲強參數錯誤地命名屬性參數。我的確是複數。

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 「/食譜」 爲:: 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「,@ 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」=>「Chicken Thighs」,「_destroy 「=>」false「},」1455332308175「=> {」name「=>」Honey「,」_des Troy「=>」false「}},」directions_attributes「=> {」1455332325877「=> {」step「=>」帶皮的棕色雞。「,」_destroy「=>」false「},」1455332325880「= > {「step」=>「添加蘋果」,「_destroy」=>「false」}}},「commit」=>「Create Recipe」} 用戶載入(0.1ms)SELECT「users」。* FROM「users 「WHERE」users「。」id「=? ORDER BY 「用戶」, 「ID」 ASC LIMIT 1 [[ 「ID」,1]] 未經許可參數:步驟 未經許可的參數:步驟 (0.1毫秒)開始事務 配方的情況下(在0.2ms)SELECT 1 AS一個從「食譜」WHERE「食譜」。「圖像」=? LIMIT 1 [[「image」,「1455332407-84040-0233/Honey-Mustard-Chicken-and-Apples.jpg」]] SQL(0.4ms)INSERT INTO「recipes」(「title」,「image」,「 VALUE(?,?,?,?,?)[[「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」 「,」雞大腿「],[」recipe_id「,1],[」created_at「,」2016-02-13 03:00:07.758440「],[」updated_at「,」2016-02-13 03:00: (「msg」,「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)實測值

發起者GET 「在2016- /食譜/ 1」 :: 1 02-12 19:00:09 -0800 通過RecipesController處理#顯示爲HTML 參數:{「id」=>「1」} 配方加載(0.3ms)SELECT「recipes」。* FROM「recipes」WHERE「食譜「。」id「=? LIMIT 1 [[「id」,1]] 用戶負載(0.2ms)SELECT「users」。* FROM「users」WHERE「users」。「id」=? LIMIT 1 [[「id」,1]] 成分負荷(0.3ms)SELECT「ingredients」。* FROM「ingredients」WHERE「ingredients」。「recipe_id」=? [[「recipe_id」,1]] 方向加載(0.1ms)選擇「方向」。*從「方向」WHERE「方向」。「recipe_id」=? [[「recipe_id」,1]] 用戶負載(0.1ms)SELECT「users」。* FROM「users」WHERE「users」。「id」=? ORDER BY「users」。「id」ASC LIMIT 1 [[「id」,1]] 佈局/應用程序中的渲染食譜/show.html.haml(9.2ms) 在47ms內完成200 OK(查看:41.2ms | ActiveRecord的:1.0ms的)

我不明白卡爾的回答了自己的問題:Rails 4.1 Nested Attributes and Fields For Getting Unpermitted Parameters and Not Saving 如果你能看到什麼,你是麥吉弗!

+0

卡爾的答案是不相關的:他自己操縱javascript/html。 @ elias-sanchez的答案對我來說似乎是正確的:你不允許在強參數中使用「step」,這很可能會導致模型被拒絕(然後它會失敗而沒有錯誤)。但很難說沒有你的食譜沒有任何型號代碼。 – nathanvda

回答

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驗證,它不會被保存。