4

I have Post table with title and content attributes. I want to make auto complete textfield where user are suggested by Post title. I am trying to add jquery auto-complete in my rails application. I am doing like this ..

controller (Adding Posts title in Array)--

  @posttitle = []
  Post.all.each do |g|
    @posttitle << g.title 
  end

View --

  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

  <%= text_field_tag :search, params[:search], :placeholder => "Search Religious Places...", :id=>"tags" %>

 <script>
   $(function() {
   var availableTags = <%= @posttitle %>;
   $( "#tags" ).autocomplete({
   source: availableTags
   });
   });
 </script>

But its not showing any suggestion (auto-complete is not working). I don't know whats going wrong. Please help

2
  • I think you should not use the ruby array in javascript. It will not evaluated as an array. Commented Jun 11, 2013 at 11:47
  • @ManojMonga I think you are right Commented Jun 11, 2013 at 12:27

3 Answers 3

8

Have you tried something like this:

<script>
   var availableTags = <%= raw @posttitle %>;
   $(function() {

        $( "#tags" ).autocomplete({
         source: availableTags
        });
   });
</script>
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for your contribution once again but still not working :(
@posttitle is a Array if I put like this <%= @posttitle %> in my html then it will show array like this ["xyz", "wxt", "etc"]
Can you try to edit first line to say $(document).ready(function() {
Problem is not $(document).ready(function() { this. When I put hard coded value like var availableTags = [ "ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme" ]; then it works fine...
hats off dude, you just save my life :) Thank you so much :) Its working
|
1

If you want an array of items in Ruby to appear as a javascript array, you'll need to:

1) get it into a comma separated list of values 2) wrap each value in quotes 3) escape the value so that quotes do not cause javascript errors

If you want just the title:

controller:

@titles = Post.pluck(:title)

and then in your view:

 <script>
   $(function() {
     var availableTags = [<%= @titles.map{|title| escape_javascript(title)}.join(", ") %>];
     $( "#tags" ).autocomplete({
       source: availableTags
     });
   });
 </script>

3 Comments

Thanks for your reply. Line @titles = Post.all.pluck(:title) should be @titles = Post.pluck(:title). I tried from this way, not working
auto-complete function is not working. Its not showing any suggestion. Well I tried to do like this <script> $(function() { var availableTags = [<%= @titles.map{|title| escape_javascript(title)}.join(", ") %>]; $('#tags').focus(function() { alert('hi'); }); }); </script> for testing purpose but alert box is not showing means line 1 is not executed correctly...
I don't think this needs its own answer, and maybe this wasn't possible when written, but another way to do this would be to use to_json so: var availableTags = <%= @titles.map(&:title).to_json.html_safe %>
0

I think you should not use the ruby array in javascript. It will not evaluated as an array. Instead you can create the javascript array and use it as

<script>
  $(function() {
    var availableTags = new Array();
    <% @posttitle.each do |post| %>
      availableTags.push(<%= post %>);
    <% end %>
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
</script>

1 Comment

I tried to put like this <script> $(function() { var availableTags = new Array(); <% @posttitle.each do |post| %> availableTags.push(<%= post %>); <% end %> $('#tags').focus(function() { alert('hi'); }); }); </script> but alert box is not showing. It means var availableTags... that line having error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.