Rails 4 Nested_form在5个问题后删除链接

时间:2015-06-16 10:56:37

标签: javascript ruby-on-rails ruby-on-rails-4 nested-forms

我正在使用嵌套的表单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个问题,我只想在有人添加问题时给出删除链接,但此链接会删除所有问题的链接, 请告诉我如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

尝试这样给它

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

答案 1 :(得分:0)

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

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