I've got a problem which seems to be quite hard to solve in Rails.
Consider having an entity Book
which contains many Pages
, and consider this Pages
having an attribute text
which contains (as strange as it may sound) text. More precisely, it is HTML code, and users are usually shown this text when accessing pages/some_page_id/show
. Instead, users are shown an "index" of all the pages of a book when they go to books/some_book_id/show
.
Now consider I want to let users "download" a book. The book will be downloaded as a zip containing an HTML file for each of its pages.
I'm quite fine with most passages, except with one: managing to render a Page
, on a file, when a BooksController
method is called.
What I've actually come up with is this line, in BooksController/show
:
def show
if params[:download]
@book.pages.each do |p|
content = render_to_string(:controller => "pages", :action => "show", :page_id => p.id)
File.open(file_name_from_book_name_and_page_number) do |f|
f.write(content)
end
end
end
This results in a file for each page... But it's content is the content of the HTML the user would see if calling BooksController/show
without any params[:download]
! That is, what is actually called is not the PagesController/show
action with a page id as argument...
How could I achieve that?
Thank you in advance!