Ruby on Rails Filters Applied to View
I'm looking to create an application that can add 'filters' to a view after it has been rendered. For example, if my view renders to:
"<html><body>demo</body></html>"
I want to capitalize all letters so it looks like:
开发者_JAVA技巧"<HTML><BODY>DEMO</BODY></HTML>"
Any ideas on how to do this? Thanks!
Simple version (in app/controllers/application_controller.rb
):
after_filter do |c|
c.response.body = c.response.body.upcase
end
However, this is probably bad since it will literally uppercase everything. You probably want to restrict it to only HTML responses and, even then, you'll want to make sure it doesn't create invalid markup (for example, it should ignore <script>
tags and the like).
精彩评论