Sunday, 15 June 2014

jquery - Initialize autocomplete on view form for Rails 3.2? -



jquery - Initialize autocomplete on view form for Rails 3.2? -

here line of code in view form attached autocomplete info source field item_name_autocomplte:

<%= f.input :item_name_autocomplete, :label => t("item name"), :input_html => { data: {autocomplete_source: suburi + base_materialx.autocomplete_parts_path}} %>

and autocomplete initialized in .js file under assets:

$(function() { homecoming $('#item_name_autocomplete').autocomplete({ minlength: 1, source: $('#item_name_autocomplete').data('autocomplete-source'), select: function(event, ui) { $(this).val(ui.item.value); } }); });

is there way above initialization code can integrated view form? when view form rendered, autocomplete initialized @ same time. autocomplete field dynamically inserted onto view user , id of element unique each insertion.

i not set js code straight view. instead, utilize content_for :smth , yield :smth within <head> tag, e.g:

# application layout: <body> ... ... <%= yield :additional_javascripts %> </body> # users/_form.html.erb: <% content_for :additional_javascripts %> <%= javascript_include_tag "some_script" %> <% end %>

with such approach, view clean , js separated. finally, some_script.js content:

$(document).ready(function() { $('#item_name_autocomplete').autocomplete({ minlength: 1, source: $('#item_name_autocomplete').data('autocomplete-source'), select: function(event, ui) { $(this).val(ui.item.value); } }); });

jquery ruby-on-rails ruby-on-rails-3 autocomplete

No comments:

Post a Comment