簡體   English   中英

Rails 4 Nested_form在5個問題后刪除鏈接

[英]Rails 4 Nested_form remove link after 5 questions

我正在使用一個嵌套表單的gem,其中有兩個字段,視圖為

<%= f.fields_for :round_questions do |question| %>
                          <%= question.label :question %>
                          <%= question.text_field :question %>
                              Rate the Answer</br></br>
                           <div class="star-questions" ></div>
                          <%= question.text_field :answer_rating, :class=> 'star-answer', :disabled=>true %>
                          <%= question.link_to_remove "Remove this Question" %>
                      <% end %>
                      <%= f.link_to_add "Add a Question", :round_questions,
                                        :class=> 'btn waves-effect waves-light btn-medium custom_btn_gray' %>

在控制器中

@interview_round = InterviewRound.where(id: params[:id]).first
    5.times {@interview_round.round_questions.build}
    respond_to do |format|
      format.js
    end

現在,當表單加載時,將出現5個問題,我只想在有人添加問題時才提供remove鏈接,但是此鏈接為所有問題提供remove鏈接,請告訴我如何解決這個問題?

嘗試像這樣給它

<%= question.link_to_remove "Remove this Question" if question.object.persisted? %>

您需要在顯示刪除鏈接時添加條件,如下所示:

<%= question.link_to_remove "Remove this Question" unless question.object.new_record? %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM