Rails4-autocomplete form with multiple inputs - HTML data-id-element overwritten

对着背影说爱祢 提交于 2019-12-02 01:45:42

问题


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.

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?

My PrescriptionsController

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

My view

[...]
        <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>
[...]

generates final 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>
    

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


回答1:


Solution by OP.

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

Fixed view file:

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

And partial _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}" %>


来源:https://stackoverflow.com/questions/23461902/rails4-autocomplete-form-with-multiple-inputs-html-data-id-element-overwritten

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