简体   繁体   English

在Rails的form_for方法内呈现不同的表单

[英]Rendering different forms inside a form_for method in rails

I have a form_for on a page in my rails app. 我的Rails应用程序中的页面上有一个form_for。 I want the user to be able to select the deal stage they want to input from a drop down menu, and for the page to respond with the corresponding form (which is in the corresponding partial). 我希望用户能够从下拉菜单中选择要输入的交易阶段,并让页面以相应的表单(位于相应的部分)进行响应。 Below is a simplified version of the form_for. 下面是form_for的简化版本。 I also have javascript which hides the 4 partials when the page is loaded, and then when one is selected from the select menu, shows that partial. 我也有一个javascript,它会在页面加载时隐藏4个部分,然后从选择菜单中选择一个时,会显示该部分。

= form_for @activity do |f|
  .field
    = f.label :select_deal_stage
    = f.select(:deal_stage, DEAL_STAGE_ARRAY[], {:selected => nil, :prompt => 'Select Stage'})
  = render "deal_stage_1_partial", :f => f
  = render "deal_stage_2_partial", :f => f
  = render "deal_stage_3_partial", :f => f
  = render "deal_stage_4_partial", :f => f

The problem here appears to be that when I fill out and submit the form, the parameters are blank, presumably because the fields are repeated within the form_for and it is submitting values from the last fields or somehow submitting values from the fields that are not actually the ones that were filled out. 这里的问题似乎是,当我填写并提交表单时,参数为空白,大概是因为这些字段在form_for中重复,并且它正在提交最后一个字段中的值,或者以某种方式从实际上不是实际的字段中提交值那些被填写。 How can I get this to work, while having the deal_stage, which is selected from the drop down menu, be one of the parameters submitted with the form? 从下拉菜单中选择deal_stage作为与表单一起提交的参数之一,我如何使其工作?

Additional Information: The partials are all different but have some shared fields. 附加信息:局部变量都不同,但是有一些共享字段。 The params ends up being a full list of all the fields across all the forms. 这些参数最终是所有表单中所有字段的完整列表。

I think you have two options: 我认为您有两种选择:

  1. Name the four sets of duplicated fields differently, eg. 用不同的名称命名四组重复的字段,例如。 if you have a item_no field, call it item_no_1, item_no_2, item_no_3, and item_no_4. 如果您有item_no字段,则将其称为item_no_1,item_no_2,item_no_3和item_no_4。 Then deal with moving the correct one to item_no. 然后处理将正确的移动到item_no。

  2. Render only one of the four views/partials at a time. 一次仅渲染四个视图/部分之一。 That would imply that the user has to select the option before you render. 这意味着用户必须在渲染之前选择选项。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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