4

I have a controller action that renders a printable version of the page using a print.html.erb template. The controller code is:

def print
    @title = 'Print - Drill'
    render :show, layout: 'print', locals: { back_pth: drill_path(@drill) }
end

and in print.html.erb there is a line:

<%= link_to 'Back', back_pth, class: 'print_link' %>

but this generates an error:

ActionView::Template::Error:
       undefined local variable or method `back_pth' for #<#<Class:0x007fd6004e1230>:0x007fd5f7da57d0>

The print template is called by many different controller actions, so how do I fix this? This code worked in rails 5.0.6.

Obromios
  • 15,408
  • 15
  • 72
  • 127

2 Answers2

2

As pointed out by FarazPantakar this is an issue in the rails 5.1 ActionController. The issue report has a workaround which is to use local_assigns in the html, so the link becomes

<%= link_to 'Back', local_assigns[:back_pth], class: 'print_link' %>
Obromios
  • 15,408
  • 15
  • 72
  • 127
0

I am not very sure if this classifies as an answer but this might be what you're experiencing?

Perhaps, for now, you could move your code to the show action as that'd work even though it doesn't work inside the layout.

FarazPatankar
  • 11
  • 1
  • 4
  • It does look like a bug introduced in rails 5.1.4. In each case, there is a corresponding show action, the idea is that when someone wants a printable version of the page, they click a link to the print action, that provides a printable version, click the 'back' link takes you back the screen viewable show action. – Obromios Feb 05 '18 at 21:54