简体   繁体   中英

Rails 4 Nested_form remove link after 5 questions

I am using a nested form gem in which i have two fields, the view is given as

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

and in the controller i have

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

now when the form loads there will be 5 questions present, I want to give the remove link only when someone adds a question but this link gives remove link to all questions, Please tell me how can I solve this ?

尝试像这样给它

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

您需要在显示删除链接时添加条件,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM