erb 代码在 javascript <script> 标签中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17043095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:52:47  来源:igfitidea点击:

erb code is not working in javascript <script> tag

javascriptruby-on-railsruby-on-rails-3erb

提问by Free-Minded

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-completein my rails application. I am doing like this ..

我有带有标题和内容属性的 Post 表。我想制作自动完成文本字段,其中用户由帖子标题建议。我正在尝试在我的 Rails 应用程序中添加jquery 自动完成功能。我正在这样做..

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

但它没有显示任何建议(自动完成不起作用)。我不知道出了什么问题。请帮忙

回答by Ermin Dedovic

Have you tried something like this:

你有没有尝试过这样的事情:

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

        $( "#tags" ).autocomplete({
         source: availableTags
        });
   });
</script>

回答by Jesse Wolgamott

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

如果您希望 Ruby 中的项目数组显示为 javascript 数组,您需要:

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

1) 将其放入逗号分隔的值列表中 2) 将每个值用引号括起来 3) 转义该值,以便引号不会导致 javascript 错误

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>

回答by Florian Dano Clement

回答by Manoj Monga

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

我认为你不应该在 javascript 中使用 ruby​​ 数组。它不会被评估为数组。相反,您可以创建 javascript 数组并将其用作

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