发出混合jQuery,Rails和Haml的问题 [英] Issue mixing jquery, rails, and haml

查看:149
本文介绍了发出混合jQuery,Rails和Haml的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将html.erb转换为html.haml.我在erb中有一个jquery.

I'm converting an html.erb into html.haml. I have some jquery in an erb which is..

:javascript
  $(".level").live("click", function() {
    //..some code
    <% if @milestone %>
      milestone = "&milestone=<%= @milestone%>";
    <% end %>
    //..some code
  });

我想将if语句转换为haml,为此,我正在做..

I want to convert the if statement into haml and for that I'm doing..

- if @milestone
  milestone = '&milestone="#{@milestone}"'

但是这不起作用,并给我"if @milestone"的语法错误

but this does not work and gives me a syntax error on "if @milestone"

我做错了什么?不能混合使用jquery和haml吗?

What am I doing wrong? Can you not mix jquery and haml?

推荐答案

:javascript过滤器(或任何过滤器)中的代码不会被视为Haml,因此您不能使用- if ...之类的东西.但是,您可以在过滤器内的#{...}中使用插值.在您的情况下,您可以执行以下操作:

Inside the :javascript filter (or any filter) code isn’t treated as Haml, so you can’t use things like - if .... However you can use interpolation with #{...} inside filters. In your case you could do something like:

:javascript
  $(".level").live("click", function() {
    //..some code
    #{"milestone = \"&milestone=#{@milestone};\"" if @milestone}
    //..some code
  });

嵌套的插值部分有点笨拙,因此您可能需要将其移到辅助方法中,例如:

That is a bit unwieldy with the nested interpolated parts, so you might want to move it into a helper method, something like:

def add_milestone(milestone)
  if milestone
    "milestone = \"&milestone=#{milestone}\";"
  else
    ""
end

然后您的Haml会看起来像

Then your Haml would look like

:javascript
  $(".level").live("click", function() {
    //..some code
    #{add_milestone(@milestone)}
    //..some code
  });

这篇关于发出混合jQuery,Rails和Haml的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆