0

I have a page in rails that has several partials rendered in it. I want many of those partials to have a link which will allow the user to print them. Currently, I can only get the print link to print the whole page rather than just the partial.

Is there a way to do this, or will I need the user to load a whole page before printing?

Edit: I would like to have each partial have its own individual printing link. There are two many link_to lines I've tried using.

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

The line above doesn't do anything when clicked.

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

The line above prints the whole page.

I was lead to believe that the second argument for link_to was the page that should be printed.

Reimus Klinsman
  • 898
  • 2
  • 12
  • 23
  • 1
    Maybe this answers your question: http://stackoverflow.com/questions/1071962/how-to-print-part-of-rendered-html-page-in-javascript – Louis XIV Dec 08 '15 at 04:30
  • 1
    Can you show the code you are using? Are you just using the `print` menu item under `file` in the browser? What do you mean by "load a whole page before printing"? This question needs more information and examples of what you are trying to achieve. – Beartech Dec 08 '15 at 04:31
  • Hey you can render that particular partial on new URL and print that URL silently using this links using iframe. – Vishal JAIN Dec 08 '15 at 06:29
  • Print version is just styling, if you can do separate controller to render print version for part of data you represent in current partial, you'll get what you want – Ivan Shamatov Dec 08 '15 at 07:06

1 Answers1

0

I do it this way:

In my view I have a link like this:

<%= link_to 'Show Current Inventory Levels', parts_inventory_levels_path, :target => '_blank'%>

The :target => '_blank' causes it to open in a new tab/window The parts_inventory_levels_path is in the routes.rb as:

get 'parts/inventory_levels'  => 'parts#inventory_levels'

So it's going to the parts controller and calling the inventory_levels action which is:

def inventory_levels
  @parts = Part.all.order(:name)
  render 'inventory_levels', layout: "print_table"
end

Here's the key part, layout: "print_table". I have a layout file in my app/views/layout/ folder called print_table.html.erb:

<head>
  <%= stylesheet_link_tag    'print', media: 'all' %>
  <%= javascript_include_tag 'application' %>
  <%= csrf_meta_tags %>
  <%= yield(:head) %>
</head>
<body>
  <%= yield %>
</body>

So the data from my controller action calls render on inventory_levels.html.erb:

<%= link_to_function('Print this Page', 'javascript:print()') %>
<br>
<table id="parts_table" class="table pretty" border='1'>
  <thead>
    <tr>
    <th class="sortable">Name</th>
    <th class="sortable">Sku</th>

  ...table omitted for brevity

Note the first line, it calls the javascript:print() function to print the page. The reason I render a separate page is I want to format that page in a very simple way that is better suited to printing. The nice thing is I am still able to use the table_sorter javascript to sort the table before I print it.

"I was lead to believe that the second argument for link_to was the page that should be printed." That is incorrect. The second part of the link_to is the path or url. Rails is interpreting your table_ingredients as

<a href="/<controller name>/table_ingredients">print ingredients</a>

I don't know your controller name so I had to use a placeholder.

I could help you write the actual code but you should just be able to use what I have here applied to your code. But if you need more help please post your controller, and any applicable routes. You can probably reduce repetitive code by passing the same controller a value as to which partial you want to render (I am rendering a whole new page but the partial should also work). Also there is probably away to use AJAX.

Beartech
  • 6,173
  • 1
  • 18
  • 41