I would like to get the html that would be sent if a client were to make a request of a certain controller action. However, I would like to do this from the server. I would like to store this raw html in a database for use at some later date. How can this be done?
E开发者_StackOverflow中文版dit: I need the extras that are sent with a response as well, stylesheets, scripts, etc...
You can get the HTML by using the NET::HTTP library within Ruby
url = URI.parse('http://www.example.com/index.html')
req = Net::HTTP::Get.new(url.path)
res = Net::HTTP.start(url.host, url.port) {|http|
http.request(req)
}
body_html = res.body
You can then save body_html
into your database object.
The catch is that this returns the html that the client would get. In the "real" world, the browser then parses this HTML and then makes separate HTTP get requests for the stylesheets, scripts, images. You'd need to do the same thing and then store those in separate database objects.
This question gives you some tools that might help with the parsing part: Method to parse HTML document in Ruby?
Word of Warning: I suspect that what you're trying to do is going to be a lot harder than you think. Give some good thought to what you're really trying to accomplish and if this is the best method for getting there.
I was hoping that I wouldn't have to do this as it seems a bit hacky, but this can be achieved by placing the desired html into a partial, then calling
render :partial => '/the/partial'
See this link for more details:
http://www.justinbritten.com/work/2008/10/rendering-rails-partials-in-a-model-or-background-task/
Edit: the method described in the link above only works pre Rails 3. For rails 3, use AbstractController to achieve rendering.
精彩评论