0

Let's say, I will have two versions of show.html.erb for the same model.

The first one, which is default, I will call as show.html.erb.

The second one, for example - show1.html.erb.

So, I want to use the 1st one to show on the browser, and 2nd one for print.

Do I have to create a method for this in the controller?

In general is it possible to create other views besides those that are created by scaffolding?

Askar
  • 5,784
  • 10
  • 53
  • 96

2 Answers2

2

You could use different formats of output using respond_to method

Let's say you will need html and text versions

def action
  # do some logic
  respond_to do |format|
    format.html
    format.text
  end
end

In this case for route ".../action" show.html.erb will be rendered. And for ".../action.txt" show.text.erb will be rendered. You could customize template name by passing them in block like format.text { render 'show1' }

Look more: http://apidock.com/rails/ActionController/MimeResponds/InstanceMethods/respond_to

Ivan Kryak
  • 66
  • 1
1

Why not use this wicked_pdf gem: https://github.com/mileszs/wicked_pdf to generate pdf for print:

Add Mime::Type.register "application/pdf", :pdf

to config/initializers/mime_types.rb

in show method:

  def show
    respond_to do |format|
      format.html
      format.pdf do
        render :pdf => "file_name"
      end
    end
  end

now you'll just need to create a show.pdf.erb file and use it for your printing.

rmagnum2002
  • 11,341
  • 8
  • 49
  • 86
  • Thanks, I found also some railscasts regarding PDF. – Askar Jul 24 '13 at 05:41
  • I knew there was one, I used it a year ago and I just couldn't find the link to put it here. – rmagnum2002 Jul 24 '13 at 05:43
  • Here's the link http://railscasts.com/episodes?utf8=%E2%9C%93&search=pdf. Which one do you mean? – Askar Jul 24 '13 at 05:46
  • Yea, I've seen this, looks like I used a tutorial that's not railscasts, anyway, this is easy to add to your app based on documentations they have. – rmagnum2002 Jul 24 '13 at 05:48
  • Thanks! If it happens you recall, please let me know. :) – Askar Jul 24 '13 at 05:50
  • well, it was this one http://railscasts.com/episodes/220-pdfkit?view=asciicast, I just got the bottom of the page and there he says about wickedpdf and decided to use that one. So I added without tutorial. I even hava a question on SO about image render in pdf, http://stackoverflow.com/questions/7777924/wicked-pdf-image-rendering take a look, you might need it. – rmagnum2002 Jul 24 '13 at 05:58