2015-10-13 87 views
0

我有一個rails 4應用程序,我嘗試填充Post和Detail模型。 模型之間的關係是has_one。 我PostController中我有:如何處理嵌套的形式和強大的參數在軌道4

def post_params 
    params.require(:post).permit(:title, :email, :tel, :detail_attributes => [:id, :description, :post_id]) 
end 

,我的形式是:

<%= f.fields_for @post.detail do |builder| %> 
    <%= render "detail_form", detail: builder%> 
<% end %> 

` 當我檢查post_params我得到: { 「標題」=> 「」, 「電子郵件」=>」 [email protected]「,」tel「=>」「}

如何將白名單詳細信息屬性?

謝謝

回答

0

請試試這個

class Post < ActiveRecord::Base 
    has_one :detail 
    accepts_nested_attributes_for :detail, allow_destroy: true 
end 

<%= form_for @ost do |f| %> 
    <%= f.fields_for :detail do |builder| %> 
    <%= render "detail_form", detail: builder%> 
    <% end %> 
<% end %> 
0

您的StrongParameters' require/permit行代碼看起來正確。就像Prashant4224回答的那樣,你是否在你的model.rb中包含了accepts_nested_attributes_for

class Post < ActiveRecord::Base 
    has_one :detail 
    accepts_nested_attributes_for :detail 
end 

旁註:如果你選擇添加allow_destroy: true,你將需要添加_destroy到您的通行證線在你的控制器像這樣:

def post_params 
    params.require(:post).permit(:title, :email, :tel, :detail_attributes => [:id, :description, :post_id, :_destroy]) 
end 

如果你已經接受了你的模型嵌套參數,您的表單可能無法發送這些參數。您應該在調用post_params之前檢查參數,並從那裏調試您的視圖代碼。

+0

我第二次檢查params和post_params這兩個概念,可以更容易地看出發生問題的位置:傳入的參數或post_params方法。 –