jQuery UI Autocomplete with a JSON datasource generated from Rails - not working -
i'm trying set input tag jquery autocomplete function, it's doesn't work when im referring external json data. works perfectly, however, local json-like array... let me explain in code:
html file:
<html> <head> <meta charset="utf-8"> <script src="jquery-1.4.2.min.js" type="text/javascript"></script> <script src="jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script> <script> $(function() { $("#birds").autocomplete({ source: "http://localhost:3000/autocomplete_searches/index.json", minlength: 3 }); }); </script> </head> <body> <div class="ui-widget"> <label for="birds">birds: </label> <input id="birds" /> </div> </body> </html>
autocomplete_searches_controller.rb in rails app
class autocompletesearchescontroller < applicationcontroller def index @tags = tag.limit(30).name_like(params[:term]) @tags_hash = [] @tags.each |tag| @tags_hash << {"label" => tag.label} end render :json => @tags_hash end end
this json action alone works well, example: http://localhost:3000/autocomplete_searches/index?term=psychiatric gives me:
[{"label":"psychiatric hospital"},{"label":"psychiatric nurse"},{"label":"psychiatric examination"}]
and can see jquery function working somehow, because when im typing example "italy" in #birds input-box webrick gives me:
started "/autocomplete_searches/index.json?term=italy" 127.0.0.1 @ 2010-09-27 18:07:07 +0200 processing autocompletesearchescontroller#index json parameteres: {"term"=>"italy"} bla bla bla select "tags".* "tags" (tags.name '%italy%') limit 30
but see no effects on website. , said, autocomplete script working when put data in same format straight in html file. in 1 im not getting problems:
<html> <head> <meta charset="utf-8"> <script src="jquery-1.4.2.min.js" type="text/javascript"></script> <script src="jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script> <script> $(function() { $("#birds").autocomplete({ source: [{"label":"psychiatric hospital"},{"label":"psychiatric nurse"},{"label":"psychiatric examination"}], minlength: 3 }); }); </script> </head> <body> <div class="ui-widget"> <label for="birds">birds: </label> <input id="birds" /> </div> </body> </html>
so where's problem? im new json maybe im doing sth wrong.
ee... made new action in rails , put html code view. started work. why couldnt working while stand alone html file?
Comments
Post a Comment