具有Ruby后端的下划线模板不起作用

时间:2022-11-05 20:46:02

I am using the underscore template to create a template for making bootstrap dropdown menus. The code worked fine on my own computer, but now I'm adding it to a ruby on rails backend server. Now the code no longer works. I was told the code in between the <% %> is interpreted as ruby code instead of javascript. I don't know ruby at all, but someone showed me how to write a for loop.

我正在使用下划线模板来创建用于制作引导下拉菜单的模板。代码在我自己的计算机上工作正常,但现在我将它添加到rails后端服务器上的ruby。现在代码不再有效了。有人告诉我,<%%>之间的代码被解释为ruby代码而不是javascript。我根本不认识ruby,但是有人告诉我如何编写for循环。

<!-- Dropdown Menu-->
<script type="text/template" id="dropdown">
  <div class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown">
      <span> <%= name %> </span>
      <b class="caret"></b>
    </a>
    <ul class="dropdown-menu">
      <% array_of_items.each do |obj| %>
          <li> <a> <%= obj %> </a> </li>
      <% end %>
    </ul>
  </div>
</script>

The error I get is "Undefined variable array_of_items" Of course it's not defined, I define it later on

我得到的错误是“未定义的变量array_of_items”当然它没有定义,我稍后定义它

this.$el.append(this.makeDropdown({
    name: this.name,
    array_of_items: [1,2,3,4]
}));

The underscore template works like this for Javascript, but for Ruby it will not allow me to have an undefined variable in a template for some reason, What can I do?

对于Javascript,下划线模板的工作方式与此类似,但对于Ruby,由于某种原因,它不允许我在模板中使用未定义的变量,我该怎么办?

2 个解决方案

#1


0  

So the problem I think is here:

所以我认为这个问题在这里:

<% array_of_items.each do |obj| %>

Rails is interpreting this as ERB, and as there's no variable declared in Ruby, you're getting the error. Try adding your JS code into the asset pipeline perhaps?

Rails将其解释为ERB,并且因为在Ruby中没有声明变量,所以你得到了错误。尝试将JS代码添加到资产管道中?

#2


0  

Javascript's and ruby's variable are in quite different 'scopes'; here <% array_of_items %> should be defined as a template local variable:

Javascript和ruby的变量是完全不同的“范围”;这里<%array_of_items%>应该被定义为模板局部变量:

<% array_of_items = [1,2] %>
<%= array_of_items %>
<script>
  document.body.innerHTML += array_of_items;
  array_of_items = ['j', 's'];
  document.body.innerHTML += array_of_items;
  // var from js + var from ruby
  document.body.innerHTML += array_of_items + <% array_of_items %>; // #=> should be smth like `js12`
</script>

#1


0  

So the problem I think is here:

所以我认为这个问题在这里:

<% array_of_items.each do |obj| %>

Rails is interpreting this as ERB, and as there's no variable declared in Ruby, you're getting the error. Try adding your JS code into the asset pipeline perhaps?

Rails将其解释为ERB,并且因为在Ruby中没有声明变量,所以你得到了错误。尝试将JS代码添加到资产管道中?

#2


0  

Javascript's and ruby's variable are in quite different 'scopes'; here <% array_of_items %> should be defined as a template local variable:

Javascript和ruby的变量是完全不同的“范围”;这里<%array_of_items%>应该被定义为模板局部变量:

<% array_of_items = [1,2] %>
<%= array_of_items %>
<script>
  document.body.innerHTML += array_of_items;
  array_of_items = ['j', 's'];
  document.body.innerHTML += array_of_items;
  // var from js + var from ruby
  document.body.innerHTML += array_of_items + <% array_of_items %>; // #=> should be smth like `js12`
</script>