您應該使用Rails caching實現這一結果。它實現了你正在尋找的目標。
或者,您可以render_to_string和輸出使用呈現結果:
#ticket_controller.rb
def TicketController < ApplicationController
def show_ticket
@ticket = Ticket.find(params[:id])
res = render_to_string :action => :show_ticket
#... cache result-- you may want to adjust this path based on your needs
#This is similar to what Rails caching does
#Finally, you should note that most Rails servers serve files from
# the /public directory without ever invoking Rails proper
File.open("#{RAILS_ROOT}/public/#{params[:action]}.html", 'w') {|f| f.write(res) }
# or .. File.open("#{RAILS_ROOT}/public/#{params[:controller]}/#{params[:action]}/#{params[:id]}.html", 'w') {|f| f.write(res) }
# or .. File.open("#{RAILS_ROOT}/snapshots/#{params[:controller]}/#{params[:action]}/#{params[:id]}.html", 'w') {|f| f.write(res) }
render :text => res
end
end
我想這就是我一直在尋找。你能給出更多這個render_to_string的用例嗎?基本上我正在尋找將頁面加載的整個輸出保存到.html文件。 –
render_to_string應該給你你正在尋找的所有的Rails將給予瀏覽器的HTML,包括佈局。您可以輕鬆將其保存到文件中。 – ghayes
這是哪裏呢?在控制器中?在視圖中? –