Rails的form_for呈现部分form_for列表值而不提交给数据库,但现在2提交 [英] Rails form_for renders partial form_for to list values without submitting to DB, but now 2 submits

查看:155
本文介绍了Rails的form_for呈现部分form_for列表值而不提交给数据库,但现在2提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的显示器 - form_for一个应用程序,它呈现另一个带有form_for的部分来显示教育资格。部分是多态的。所以,现在我有2个提交,主要提交根本没有回应和部分作品....为什么?请注意,代码适用于一条记录,但需要按部分表单提交。请帮助。

 <%= bootstrap_form_for @application do | f | %GT; 
< div class =panel panel-default>
< div class =panel-heading>教育:< / div>
< div class =panel-body>
<%=呈现部分: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%>

部分form_for是:(注意:我甚至拿出submit并在主表单上使用link_to,如图所示以上但仍无效。

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

所需的行为必须是:


  1. 部分显示,显示带有学士/硕士/博士学位的投递箱和
    另一个文本字段以接受资质名称。

  2. 按钮/链接 - '添加更多资格'给列出资格条件而不提交给DB。

  3. 只有主表单按钮应该最终将申请表格提交给DB。

我向经验丰富的开发人员提出的问题:

帮助列在主表单上。如果是的话,如何帮助。所有的AJAX代码,我找到了SO和谷歌提交数据库,而不是简单地列出。



<2>替代重新部分再次用主窗体中的按钮可以提交部分帮助简单列出值?

解决方案

好的,我终于解决了这个问题,这对未来的访问者会有帮助。通过进行2次更改,两个想法都被记入了Stackoverflow开发人员的2个不同的问题: -
$ b


  1. Form_for在不同部分中输入和提交按钮 - @Anand

  2. Rails将form_for对象传递给partial - @ D.Patrick

Thx给你们俩。所以,最后我现在使用render调用的主窗体看起来如下:

 <%= bootstrap_form_for @application do | f | %GT; 
*许多其他应用程序字段,但在这里无关。*
< div class =panel panel-default>
< div class =panel-heading>教育:< / div>
< div class =panel-body>
<%@form = f%>
<%=呈现部分: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%>

和呈现的部分形式:

 <%= f.fields_for [degreeable,Degree.new],:url => {:action => index} do | ff | %GT; 
< 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.submitAdd Another Degree,class:'btn btn-primary'%>
<%end%>

但是我仍然坚持使用JS / JQuery的点击按钮来列出部分fields.Have发布了这个问题现在在StackOverflow上


更新


我发布了完整的答案在这个链接


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?

解决方案

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.

这篇关于Rails的form_for呈现部分form_for列表值而不提交给数据库,但现在2提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆