具有多个输入的Rails4

具有多个输入的Rails4

本文介绍了具有多个输入的Rails4-autocomplete表单-覆盖了HTML数据ID元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个表单,该表单将从数据库中建议药物名称,但将发送所选药物的ID以在我的数据库中创建关联.

I am trying to build a form that is going to suggest medicine name from database but will send ID of chosen medicine to create relation in my database.

我使用了自动完成的gem,效果很好,我也使用了它们的提示来从元素名称中获取ID,这也可以正常工作,但只适用于表单上只有一个INPUT元素的情况.在我的情况下,我需要5个输入,并且由于我的代码-它一直覆盖称为my_medicine_id的覆盖元素,这导致只保存了一个-last-元素.你们能想到用于动态更改字段名称的任何解决方案吗?

I used autocomplete gem which works great, I also used their hint for getting ID out of elements name and this works fine as well but only to the point where there is ONE INPUT element on form. In my case I need 5 inputs and because of my code - it keeps overwritting element called my_medicine_id which causes that only one - last - element is being saved. Can you guys think of any solution for dynamically changing field name?

我的PrescriptionsController

My PrescriptionsController

[...]
    def new
        @prescription =Prescription.new
        5.times { @prescription.relations.build }
    end
[...]

我的观点

[...]
        <ol>
            <%= f.fields_for :relations do |builder| %>
                <%= builder.hidden_field :medicine_id, :id => "my_medicine_id" %>
                <%= builder.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => '#my_medicine_id' %>
            <% end %>
        </ol>
[...]

生成最终的html:

<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][0][medicine_id]"></input>
<input id="prescription_relations_attributes_0_medicine_name" type="text" rows="5" name="prescription[relations_attributes][0][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][1][medicine_id]"></input>
<input id="prescription_relations_attributes_1_medicine_name" type="text" rows="5" name="prescription[relations_attributes][1][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][2][medicine_id]"></input>
<input id="prescription_relations_attributes_2_medicine_name" type="text" rows="5" name="prescription[relations_attributes][2][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][3][medicine_id]"></input>
<input id="prescription_relations_attributes_3_medicine_name" type="text" rows="5" name="prescription[relations_attributes][3][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>
<input id="my_medicine_id" type="hidden" rows="5" name="prescription[relations_attributes][4][medicine_id]"></input>
<input id="prescription_relations_attributes_4_medicine_name" type="text" rows="5" name="prescription[relations_attributes][4][medicine_name]" data-id-element="#my_medicine_id" data-autocomplete="/relations/autocomplete_medicine_name"></input>

因此您可以看到每次它覆盖元素data-id-element="#my_medicine_id".

So as you can see each time it overwrites element data-id-element="#my_medicine_id".

推荐答案

OP解决方案.

找到了解决方案-在fields_for内部移动了一部分,并使用了f.options[:child_index].

Solution found - moved inside of fields_for to a partial and used f.options[:child_index].

固定的视图文件:

[...]
            <%= f.fields_for :relations do |builder| %>
                <%= render 'child_form', :f => builder %>
            <% end %>
[...]

和部分_child_form.html.erb

<% @it=f.options[:child_index] %>
<%= f.hidden_field :medicine_id, :id => "my_medicine_id#{@it}" %>
<%= f.autocomplete_field :medicine_name, autocomplete_medicine_name_relations_path, :id_element => "#my_medicine_id#{@it}" %>

这篇关于具有多个输入的Rails4-autocomplete表单-覆盖了HTML数据ID元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:30