3
def foo    
  respond_to do |format|
    format.html { render layout: 'application' }
    format.pdf { 
      render pdf: 'foo', layout: 'pdf'
    }
  end
end

if I put only hello world in foo.pdf.erb it works !

but now if I want use a html view to iterate on my model (from DB) like :

<p>hello world</p>    
<%= render @foo.chapters.order(number: :asc) %>

it doest works, the page stay grey, no error in log...

if i comment this line, hello world works again.

So the issu is about using html partial : how can we use the normal html view in pdf view?

Matrix
  • 3,458
  • 6
  • 40
  • 76

1 Answers1

5

I tried to reproduce your issue in this commit of the Wicked PDF issues project and was successful in accomplishing what you show here, however there is a minor caveat.

Using the Rails model rendering shorthand:

# assuming @books = Book.all or some collection of Book objects
<%= render @books %>

It will try and render that collection with the extension of the request.

Meaning, for an html request, it will attempt to render the template at:

app/views/books/_book.html.erb

And for a PDF request, it will attempt to render

app/views/books/_book.pdf.erb

I worked around this by renaming my partial to:

app/views/books/_book.erb

So it will be used regardless of an HTML or PDF request.

If that doesn't work in your situation, you can create a new PDF partial, that renders the HTML one like this:

# app/views/books/_book.pdf.erb
<%= render partial: '/books/book',
      formats: [:html],
      locals: { book: book } %>
Unixmonkey
  • 18,485
  • 7
  • 55
  • 78