2

enter image description hereI'm not sure if it's related to Rails, JavaScript or more just to some basic old HTML code.

I have a simple web app I've built with Ruby on Rails with a navbar and a sidebar at:

layout > application.html.erb

And the actual page (<%= yield %>) in the middle (classic bootstrap style I copied from bootstrap main example).

How can I add a simple Print button that lets the user print just the the actual page that the button is located at (without the navbar and sidebar)?

Commando
  • 358
  • 3
  • 16

1 Answers1

2

here you can use

<%= link_to 'print', 'your_link_here', :onclick => 'window.print();return false;'%>

use below to hide contents you don't want to print :-

@media print {
  div.sidebar, div.navbar {
    display: none;
  }
}
Anant Vikram Singh
  • 538
  • 1
  • 4
  • 14
  • Thanks, it almost worked, the navbar did disappear from the print window, but somehow the sidebar is still there. Is there any other way to do that? maybe with a class on a div and just print that div or adding some css to hide that sidebar? – Commando Mar 17 '20 at 03:35
  • can you post the picture of `what are you expecting` and `what are you getting`. as it's hard to provide solutions without seeing what's going on – Anant Vikram Singh Mar 17 '20 at 03:37
  • Yes I added a screenshot to the question above. – Commando Mar 17 '20 at 03:52
  • if you are using `sidebar` and `navbar` as different components, try above line of code inside the component you want to print. Maybe that can help you – Anant Vikram Singh Mar 17 '20 at 04:12
  • I don't know why I can't comment the answer on here but I found this: https://stackoverflow.com/questions/2255291/print-the-contents-of-a-div as the easiest way to print just a specific div, no CSS involved. – Commando Mar 17 '20 at 15:45
  • question is closed so you are not able to comment – Anant Vikram Singh Mar 18 '20 at 01:40