简体   繁体   English

Ruby On Rails:引用同一对象的表单字段

[英]Ruby On Rails: Form fields that refer to the same object

I have a form with several fields that are associated with the same object_name :result . 我有一个表单,其中包含与同一object_name :result关联的多个字段。 I have some jquery/javascript that hides all but one of these fields depending on a field that is chosen earlier in the form. 我有一些jquery / javascript,它会根据表单中较早选择的字段隐藏除这些字段之一以外的所有字段。 How can I ensure that when I hit the submit button, the :result object is given the correct value, depending on which field is actually shown? 如何确保在我单击“提交”按钮时,根据实际显示的字段,给:result对象正确的值? It seems that since all the other fields are just hidden, rails gets confused as to which :result should be used. 似乎由于所有其他字段都只是隐藏的,所以Rails对于应该使用哪个:result感到困惑。

<div class="Collection1 desc">

    <div id="pf" class="result" style="display: none;">
    <%= f.label :result %>
    <%= f.radio_button :result, 'pass' %> Pass
    <%= f.radio_button :result, 'fail' %> Fail
    <br><br>
    </div>

    <div id="int" class ="result" style="display: none;">
    <%= f.label :result %>
    <%= f.number_field :result %>
    </div>

    <div id="text" class ="result" style="display: none;">  
    <%= f.label :result %>
    <%= f.text_field :result %>
    </div>

    <div id="area" class ="result" style="display: none;">
    <%= f.label :result %>
    <%= f.text_area :result %>
    </div>

</div>

You should also disable the fields that you are hiding. 您还应该禁用要隐藏的字段。 This way they will not get submitted with the form. 这样,他们将不会与表单一起提交。

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

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