HAML 中 Javascript 中的 Ruby 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2640863/
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-08-23 01:16:25  来源:igfitidea点击:

Ruby methods within Javascript within HAML

javascriptjqueryrubyhaml

提问by Josh

I have a jQuery script that adds a new field to a form, and this field contains dynamic information from an array. The problem is that I can't figure out how to add an array.each to populate the options of the select field within the javascript without breaking the HAML indentation and causing errors.

我有一个向表单添加新字段的 jQuery 脚本,该字段包含来自数组的动态信息。问题是我不知道如何添加一个 array.each 来填充 javascript 中选择字段的选项,而不会破坏 HAML 缩进并导致错误。

Here is my best attempt that does not work:

这是我最好的尝试,但不起作用:

%script(type="text/javascript")  
  $('#mylink').click(function() {  
  $('#mylink').after('<select>  
  - myarray.each do |options|  
     <option value="#{options.id}">#{options.name}</option>  
  </select>);  
  )};

Also tried it with the :javascript filter with no luck.

还用 :javascript 过滤器尝试过,但没有运气。

回答by Alex Wayne

Usually, if something is a pain in haml, it means you should refactor the the tricky bit to a helper or partial and call that.

通常,如果某些事情在haml 中很痛苦,这意味着您应该将棘手的部分重构为帮助程序或部分并调用它。

// some_helper.rb
def new_snazzy_select_tag(options = [])
  select_tag 'tag_name_here', options.map { |option| [option.id, option.name] }
end

Also, you should use the :javascriptfilter to render javascript since it will put it in a script tag for you and allow indentation.

此外,您应该使用:javascript过滤器来呈现 javascript,因为它会将其放入脚本标记中并允许缩进。

Lastly, you can use #{ruby_expression}anywhere in haml, including :javascriptfilters, which is very handy when you need to output the result of ruby expressions to places that are not directly contents of html elements.

最后,您可以#{ruby_expression}在 haml 中的任何地方使用,包括:javascript过滤器,当您需要将 ruby​​ 表达式的结果输出到不是 html 元素直接内容的地方时,这非常方便。

// some_view.html.haml
:javascript
  $('#mylink').click(function() {  
    $('#mylink').after("#{escape_javascript new_snazzy_select_tag(myarray)}");
  };

回答by ehsanul

Try this, it should work (all i did was remove a single space in the fifth line and add the closing quote on the sixth):

试试这个,它应该可以工作(我所做的只是删除第五行中的一个空格并在第六行添加结束引号):

%script(type="text/javascript")
  $('#mylink').click(function() {
  $('#mylink').after('<select>
  - @myarray.each do |options|
    <option value="#{options.id}">#{options.name}</option>
  </select>');
  )};

However, assuming you're running this with a ruby script or framework of sorts, why not just do it outside the template? That would probably be most appropriate. In rails/sinatra and other frameworks, you could use a helper method to do this. If you look at the haml reference, they actually discourage use of -to evaluate ruby.

但是,假设您使用 ruby​​ 脚本或各种框架运行它,为什么不在模板之外执行它呢?那可能是最合适的。在 rails/sinatra 和其他框架中,您可以使用辅助方法来执行此操作。如果您查看 haml 参考资料,他们实际上不鼓励使用-来评估 ruby​​。