Get rails view helper methods inside coffeescript

落花浮王杯 提交于 2019-12-12 11:22:07

问题


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

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

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 %>

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.

If I put a simple

alert 'this should appear'

inside my some_test_page.js.coffee file it works fine.

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/8609030/get-rails-view-helper-methods-inside-coffeescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!