本文介绍了在coffeescript中获取rails视图帮助器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个视图助手,请说(为了简单起见)

I have a view helper, let say (for simplicity's sake)

def call_alert
  return "alert 'this should appear'"
end

然后我有一个coffeescript文件 some_test_page。 js.coffee ,用于通过ajax调用的操作。
在此 some_test_page.js.coffee 内有:

Then I have a coffeescript file some_test_page.js.coffee which renders for an action which is called via ajax.Inside this some_test_page.js.coffee I have:

<%= call_alert %>

当我对 / some_test_page 进行ajax调用时,从coffeescript文件获得带有编译的javascript的响应,并且将发生警报。

When I do an ajax call to /some_test_page, I usually would expect to get a response with the compiled javascript from the coffeescript file and an alert would occur. However, it seems the view helper I tried to use cannot be accessed.

如果我把一个简单的

alert 'this should appear'

咖啡文件它工作正常。

我觉得这应该是确定,但也许我正在做一些反约定或愚蠢的?

I feel like this should be ok, but maybe I'm doing something counter-convention or stupid?

如何从我的coffeescript视图文件中的application_helper.rb获取视图助手?

How can I get the view helpers from application_helper.rb inside my coffeescript view files?

推荐答案

似乎问题是,我的帮助方法中的javascript没有添加换行符到它正在返回 doh

It seems the issue was that the javascript inside my helper method wasn't adding newlines to the js it was returning doh.

Coffeescript试图编译一个巨大的未分离的代码语句字符串。
我添加了\\\
到我的js代码行的助手,一切工作正常现在。

As a result, Coffeescript was trying to compile one huge string of unseparated code statements.I added \n to my js code lines in the helper and everything works fine now.

这篇关于在coffeescript中获取rails视图帮助器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 11:31