开发者

Rails 3: Insert HTML-Snippets into responses within a gem

开发者 https://www.devze.com 2023-03-06 06:17 出处:网络
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 ge

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.

  1. 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.

  2. 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 woul开发者_StackOverflow社区d 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

0

精彩评论

暂无评论...
验证码 换一张
取 消