2017-12-28 27 views
1

에 대한 또 다른 필드 내에서 렌더링되지 값에 대한 필드 : 내 card_fields_render 부분에서레일 코쿤 보석 : 나는 카드를 추가 할 <a href="https://github.com/nathanvda/cocoon" rel="nofollow noreferrer">the Cocoon Gem</a>를 사용

<%= f.fields_for :cards do |card_fields| %> 
           <% if @there_is_card %> 
            <%= card_fields.object.buttons.build %> 
            <%= render 'card_fields_render', f: card_fields %> 
           <% end %> 
          <% end %> 

를, 내가 추가 (여부)하는 또 다른 필드가 카드에 버튼을 : 카드가 버튼으로 렌더링 될 때

... 
<%= f.fields_for :buttons do |button_card_fields| %> 
     <div class="add-button-card-modal"> 
     <h4>Add New Button</h4> 
     <label>Button Text</label> 
     <%= button_card_fields.text_field :button_text, :maxlength => 20, placeholder: "Enter the text to display on the button..." %> 
     <br><br> 
     <label>Button URL</label> 
     <%= button_card_fields.text_field :button_url, placeholder: "Paste URL..." %> 
     <div class="nav-popups-buttons"> 
      <button type="button" id="validate_new_card_button" class="small-cta2">Add Button</button> 
      <p class="remove-link" id="delete_new_card_button">Remove Button</p> 
     </div> 
     </div> 
    <% end %> 
... 

은 불행하게도, 값은 button_textbutton_url 필드에 표시되지 않습니다.

enter image description here

나는 f.object.buttons.build를 추가하려고 노력하지만 문제가 해결되지 않습니다.

카드 모델 :

class Card < ApplicationRecord 
    validates :remote_image_url, :format => URI::regexp(%w(http https)), presence: { message: '%{value} : please enter valid url' }, :allow_blank => true 
    validates :title, :subtitle, :presence => true 

    belongs_to :letter, optional: true 
    has_many :buttons, dependent: :destroy, inverse_of: :card 
    has_many :analytic_clics, dependent: :destroy 

    accepts_nested_attributes_for :buttons, :reject_if => Proc.new { |att| att[:button_text].blank? && att[:button_url].blank? }, allow_destroy: true 
end 

버튼 모델 : 컨트롤러

class Button < ApplicationRecord 
    validates :button_url, :format => URI::regexp(%w(http https)), presence: { message: '%{value} : please enter valid url' }, 
    unless: Proc.new { |a| a.button_url.blank? } 
    validates :button_text, :presence => true, unless: Proc.new { |a| a.button_url.blank? } 

    belongs_to :message, optional: true 
    belongs_to :card, optional: true 
    has_one :short_url, dependent: :destroy 
    has_many :analytic_clics, dependent: :destroy 
end 

문자 PARAMS :

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: [:id, :content, :_destroy, buttons_attributes: [:id, :button_text, :button_url, :_destroy]], 
     cards_attributes: [:id, :title, :subtitle, :image_url, :remote_image_url, :button_share, :_destroy, buttons_attributes: [:id, :button_text, :button_url, :_destroy]]) 
    end 

문제가 무엇 어떤 생각? 어쩌면 누에 고치의 link_to_add_association을 사용하여 단추를 추가해야합니까? 문제는 카드를 추가하는 데 사용되는 link_to_add_association의 컨테이너에있을 것이므로 콜백 (두 번 호출 됨)에 문제가 발생한다는 것입니다. 두 번 호출됩니다.

+0

버튼과 카드 클래스를 게시 할 수 있습니까? 버튼 텍스트에서 무엇을 기대합니까? 기본값을 가지고 있습니까? –

+0

카드 및 단추 모델로 답변을 업데이트했습니다. 버튼 텍스트에서 값은 새보기에서 사용자가 입력 한 값이어야합니다. 이 경우 나는 "텍스트"입력과 랜덤 URL로 채웠다. – AlphaNico

+0

버튼이 데이터베이스에 올바르게 저장되어 있습니까 (예 : 버튼에 값이 있습니까)? 귀하의 연상은 정확합니다. 강력한 매개 변수 정의는 어떻습니까? 'card_fields.object.buttons.build'는 항상 하나의 빈 버튼을 보여줄 것입니다. 이것은'new'와 아마도'edit'에 컨트롤러를 추가하는 것을 선호합니다 (그러나 제약 조건은 다를 수 있습니다). – nathanvda

답변

1

이 제거되어 작동합니다. 버튼 필드를 두 번 생성하여 값으로 채워진 필드를 숨 깁니다.