erb code is not working in javascript <script> tag

Fre*_*ded 3 javascript ruby-on-rails erb ruby-on-rails-3

我有Post表,其中包含标题和内容属性.我想制作自动完整的文本字段,其中用户是由帖子标题建议的.我想在我的rails应用程序中添加jquery自动完成.我这样做..

controller(在数组中添加帖子标题) -

  @posttitle = []
  Post.all.each do |g|
    @posttitle << g.title 
  end
Run Code Online (Sandbox Code Playgroud)

查看 -

  <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>
Run Code Online (Sandbox Code Playgroud)

但它没有显示任何建议(自动完成不起作用).我不知道什么是错的.请帮忙

Erm*_*vic 7

你尝试过这样的事情:

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

        $( "#tags" ).autocomplete({
         source: availableTags
        });
   });
</script>
Run Code Online (Sandbox Code Playgroud)