如何使用Rails 3.1在JavaScript中调用View Helper方法 [英] How to Call a View Helper Method in JavaScript using Rails 3.1

查看:87
本文介绍了如何使用Rails 3.1在JavaScript中调用View Helper方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要渲染时间的简单应用程序.但是时间必须按照time_ago_in_words帮助程序的方式进行格式化.我从JSON对象获取时间数据.是否可以在JavaScript文件中触发视图助手(time_ago_in_words)?我正在使用Rails 3.1

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

谢谢.

推荐答案

您不能从javascript函数中执行此操作.

You can't do it from inside a javascript function.

理想情况下,您需要一个与XML Builder等效的JSON,您可以在其中从.builder部分内部调用视图帮助器方法. 此gem提供了类似的功能 https://github.com/lassebunk/api_builder .

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.

这篇关于如何使用Rails 3.1在JavaScript中调用View Helper方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆