2017-10-29 7 views
0

私は、メッセージとボタンが関連付けられたいくつかのデータを保存するレターフォームを持っています。私は、メッセージfield_for内のメッセージfield_forとボタンfields_forを使用します。入力は表示されますが、送信すると、ボタンのデータは保存されません。Rails:field_for in fields_forを保存しない

私はレールコンソールでこれを取得:

許可されていないパラメータ:ボタン

マイ形式:

def letter_params 
     params.require(:letter).permit(:campaign_name, :core_bot_id, :nb_recipients, :scheduled_at, 
     filters_attributes: [:id, :gender, :creation_date_start, :creation_date_finish, :first_name, :last_name, :segment => [], :timezone => [], :locale => []], 
     messages_attributes: [Message.attribute_names.map(&:to_sym).push(:_destroy), buttons_attributes: [Button.attribute_names.map(&:to_sym).push(:_destroy)]], 
     cards_attributes: Card.attribute_names.map(&:to_sym).push(:_destroy)) 
    end 
<%= f.fields_for :messages do |messages_fields| %> 
      <div id="text-messenger"> 
       <div id="DIV_1"> 
       <%= messages_fields.text_area :content, :required => 'required', class: "autoExpand", id: "intro-text-input", :maxlength => 640, placeholder: "Enter your text...", data: { 'rows' => '3', 'data-min-rows' => '3' } %> 
       </div> 
       <div id="add-message-button" class="add-button">+ Add Button</div> 
       <%= messages_fields.fields_for :buttons do |button_message_fields| %> 
       <div class="add-button-modal"> 
        <h4>Enter the URL and the text to display on your button</h4> 
        <label>Button Text</label> 
        <%= button_message_fields.text_field :button_text, :maxlength => 20, placeholder: "Enter the text to display on the button..." %> 
        <br><br> 
        <label>Button URL</label> 
        <%= button_message_fields.text_field :button_url, placeholder: "Paste URL..." %> 

        <button type="button" id="validate_new_message_button">Add Button</button> 
        <p class="remove-link" id="delete_new_button">Remove Button</p> 
       </div> 
       <% end %> 
      </div> 
      <% end %> 

レターコントローラでの私の許可パラメータ

レターモデル:

class Letter < ApplicationRecord 
    validates :campaign_name, :presence => true 

    belongs_to :core_bot 
    has_many :messages, dependent: :destroy 
    has_many :cards, dependent: :destroy 
    has_many :filters, dependent: :destroy 
    has_many :analytic_deliveries, dependent: :destroy 
    has_many :analytic_reads, dependent: :destroy 
    has_many :analytic_sends, dependent: :destroy 


    accepts_nested_attributes_for :filters 
    accepts_nested_attributes_for :messages 
    accepts_nested_attributes_for :cards 
end 

メッセージモデル:

class Message < ApplicationRecord 
    belongs_to :letter, optional: true 
    has_one :button, dependent: :destroy 

    accepts_nested_attributes_for :button 
end 

ボタンモデル:メッセンジャーモデルで

class Button < ApplicationRecord 
    belongs_to :message, optional: true 
    belongs_to :card, optional: true 
end 

答えて

1

は、あなたが(ない複数で)has_one button関係を設定し、それはのparamsに意味buttons_attributesの代わりにbutton_attributesにする必要があります。他の人には、あなたの属性をすべてparamsに入れないようにしてください。いくつかの属性はサーバー側で計算されるため、クライアントがそれらを埋め込む必要はありません。これが役立つことを望みます。

+0

ありがとうございます! – AlphaNico

関連する問題