Rails 3:将 HTML 片段插入 gem 内的响应中
我确实有一个关于 Rails 3 gem 开发的问题。
我想将特定的 HTML 片段/部分/片段(例如固定位置上的表单或图像)插入到每个 HTTP get 响应中。我想知道什么技术最适合这个用例。我看到两种解决方案,但我不确定哪一种是更好的方法。
由 Rails 引擎加载的机架中间件:我可以编写一个机架中间件来解析响应 HTML 文档并在结束正文标记之前插入我的 HTML 片段/部分。这种方法似乎有点脏,因为无法保证响应文档的正确格式。
在控制器级别插入代码片段:也许作为 after_filter?!这里的问题是,我必须以某种方式保证我的 after_filter 将是过滤器链中的最后一个。
我很好奇是否还有其他方法以及您会选择哪一种。如果能够从我计划插入到响应中的部分/片段中访问标准 Rails 视图助手,那就太好了。通过加载 gem,片段应该自动包含到每个响应中,而不需要用户在视图中插入部分内容。
预先感谢
彼得
I do have a question concerning the development of a gem for Rails 3.
I would like to insert specific HTML snippets/partials/fragments (e.g. a form or an image on a fixed position) into every HTTP get response. I am wondering what technique would be the most appropriate for this use case. I see two solutions, but I am not sure which one will be the better approach.
Rack middleware loaded by a Rails engine: I could write a rack middleware that parses the response HTML document and insert my HTML snippets/partials before the closing body tag. This approach seems to be a little bit dirty, since the proper formatting of the response document is not guaranteed.
Inserting the snippet at the controller level: Maybe as an after_filter?! The problem here is that I somehow have to guarantee that my after_filter will be the last one in the filter chain.
I am curious whether there are further approaches and which one you would pick. It would be great to have access to the standard Rails view helpers from within the partial/snippet I am planning to insert into the response. By loading the gem the snippet should be automatically included into every response without requiring the user to insert a partial at the views.
Thanks in advance
Peter
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论