问题
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