Rails form_for renders partial form_for to list values without submitting to DB, but now 2 submits

旧街凉风 提交于 2019-12-12 03:39:44

问题


I have a simple display - form_for an application that renders another partial with form_for for displaying education qualifications. The partial is polymorphic. So, now I have 2 submits, the main submit simply doesn't respond and partial works....why?? Please note that code works well for one record but with pressing partial form submit. Please help.

<%= bootstrap_form_for @application do |f| %>
    <div class="panel panel-default">
      <div class="panel-heading"> Education: </div>
      <div class="panel-body">
        <%= render partial: "degrees/form", :locals => { :degreeable => @application, :f => f }  %> <br>
        <%= f.link_to (fa_icon 'plus').to_s + " Add Another Qualification ", render(:partial => 'degrees/form', :locals => { :degreeable => @job, :f => f }), class: "btn btn-primary" %>
      </div>
    </div>
  <% end %>

Partial form_for is: (Note: I even took out submit and used a link_to on main form as shown above but still no effect.

<%= form_for [degreeable, Degree.new], :url => { :action => "index" } do |f| %>
  <div class = "form-group" }>
    <%= f.select :level, options_for_select(Application::EDUCATION, params[:level]), include_blank: "Select Degree", class: 'span-2' %>
    <%= f.text_field :description, :class => 'span5' %>
  </div>
 f.submit "Add Another Degree", class: 'btn btn-primary'
<% end %>

The desired behaviour must be:

  1. partial displayed to show drop box with Bachelor/Master/Ph.D and another text field to accept name of qualification.
  2. a button/link - 'Add more qualifications' to list qualifications without submitting to DB.
  3. Only main form button should finally submit form for application into DB.

My questions to experienced developers:

1.Can AJAX in partial help list down on main form. if yes, how pls help. All AJAX code I find on SO and google submit in DB and not to simply list down.

2.Instead of reloading partial again with a button from main form can submit of partial help in simply listing down values?


回答1:


Ok so I finally did resolve it and this will be helpful for future visitors. By making 2 changes both ideas are credited to Stackoverflow developers for 2 different questions:-

  1. Form_for inputs and submit button in different partials - @Anand
  2. Rails passing form_for object to partial - @D.Patrick

Thx to both of you. So, finally my main form with render call now looks:

   <%= bootstrap_form_for @application do |f| %>
      *Many other application fields but irrelevant here.*
      <div class="panel panel-default">
        <div class="panel-heading"> Education: </div>
        <div class="panel-body">
          <% @form = f %>
          <%= render partial: "degrees/form", :locals => { :degreeable => @application, :f => @form }  %> <br>
          <%= f.link_to (fa_icon 'plus').to_s + " Add Another Qualification ", render(:partial => 'degrees/form', :locals => { :degreeable => @application, :f => f }), class: "btn btn-primary" %>
        </div>
      </div>
    <%= f.submit class: "btn btn-primary btn-outline btn-sm" %>
  <% end %>

and rendered partial form:

    <%= f.fields_for [degreeable, Degree.new], :url => { :action => "index" } do |ff| %>
  <div class = "form-group" }>
    <%= ff.select :level, options_for_select(JApplication::EDUCATION, params[:level]), include_blank: "Select Degree", class: 'span-2' %>
    <%= ff.text_field :description, :class => 'span5' %>
  </div>
   <% ff.submit "Add Another Degree", class: 'btn btn-primary' %>
<% end %>

But am still stuck with JS/JQuery of click on button to list partial fields.Have posted the question now on StackOverflow.

UPDATE

: I posted the complete answer at this link.



来源:https://stackoverflow.com/questions/42432861/rails-form-for-renders-partial-form-for-to-list-values-without-submitting-to-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!