How to Call a View Helper Method in JavaScript usi

2019-08-29 02:55发布

问题:

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.