How to Call a View Helper Method in JavaScript using Rails 3.1

故事扮演 提交于 2020-01-06 09:56:43

问题


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

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