问题
I'm working on a simple app that needs to render a time. But the time must be formatted the way time_ago_in_words helper does. I'm getting the time data from a JSON object. Is it possible to trigger a view helper (time_ago_in_words) inside a JavaScript file? I'm using Rails 3.1
Thanks.
回答1:
You can't do it from inside a javascript function.
Ideally you would want a JSON equivalent to XML Builder where you could call the view helper methods from inside a .builder partial. This gem provides similar functionality https://github.com/lassebunk/api_builder.
来源:https://stackoverflow.com/questions/7572576/how-to-call-a-view-helper-method-in-javascript-using-rails-3-1