Rails:fields_for in fields_for not saving

时间:2017-10-29 13:34:11

标签: ruby-on-rails forms

我有一个Letter表单,通过Message和Button关联来保存一些数据。我在Message fields_for中使用Message fields_for和Button fields_for。显示输入,但是当我提交时,不保存按钮数据。

我在rails控制台中得到了这个:

  

未经许可的参数:按钮

我的表格:

<%= 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 %>

我在Letter控制器中允许的参数:

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

信函模特:

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 个答案:

答案 0 :(得分:1)

在信使模型中,您设置has_one button关系(不是复数形式),这意味着在params中您应该button_attributes而不是buttons_attributes。另一个建议,不要放params中的所有属性,只有那些你想要在表单中允许的属性,因为有时候,某些属性是在服务器端计算的,你不需要客户端来填充它们。我希望这对你有帮助。