如何从 Ruby on Rails 中的 RJS 助手访问视图助手?
我在多个 RJS 文件中重复了一些 RJS 的常见部分,因此想将其放入帮助程序中。
我向 app/helpers/application_helper.rb 添加了一个方法,就像我通常为 html.erb 文件添加的方法一样。该方法有效,但它无法调用 RJS 文件可以调用的视图助手。
RJS 文件中有效的行之一是:
notices = flash.collect { |type,msg| content_tag(:p, msg, :class => type) }
但是 content_tag 无法从 RJS 帮助程序访问。为什么不呢?我如何从那里访问它?
I had some common bits of RJS that I was repeating in multiple RJS files, and so wanted to put it into a helper.
I added a method to app/helpers/application_helper.rb just like I would normally for an html.erb file. The method works, except that it can't call view helpers that the RJS file could.
One of the lines that worked in the RJS file was:
notices = flash.collect { |type,msg| content_tag(:p, msg, :class => type) }
But content_tag is not accessible from the RJS helper. Why not, and how do I access it from there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的事情怎么样?
您也必须在从 rjs 调用的任何方法中使用
view_helpers
,这不是很好,但我在模型中尝试过它(仅在 2.3 和 3.1 中获得项目)并且有效...what about something like this?
You have to use
view_helpers
in any method you call from rjs too, which is not very nice, but I tried it in the model (got projects only in 2.3 and 3.1) and that worked...