Working on some legacy code (Rails v2.3) and I've become stuck.
I have a view that uses the embed tag to run an action that produces a PDF. Copying code from the other working view in my project, I have the embed code link to the controller action:
<embed height="400px" width="95%" name="plugin" src="<%= formatted_report_reports_path(:pdf) %>" type="application/pdf" />
The src links to an action called report
that needs the values for some params on the view called params[:startdate]
and params[:enddate]
, but those become nil
in the def report
action once the embed code runs.
I proved to myself that the embed
tag works by hardcoding some date values into the def report
action, so at least I know that works when it does actually have dates.
I thought I could pass the params[:]
to the src like this:
src="<%= formatted_report_reports_path(params[:startdate], :pdf) %>"
but that returns an error like this:
undefined method 'has_key?' for :pdf:Symbol
In my research I found that the :pdf
symbol is what is used for determing the "format" like what's in my default connections routes. I tried adding another connection for using the :startdate
symbol along with the format symbol, but that also didn't work:
map.connect ':controller/:action/:startdate.:format'
It had been my understanding that params[:]
keep their values throughout the program, and I've hit a wall understanding why they're nil once the embed code runs the action. Some other threads I came across mention that once another :action
is called in a :controller
the params values are wiped until they're set again, and I guess that's what's happening, but how do I set them again so that the embed code will have it?
Code I'm looking at:
report.html.erb
<%- form_tag report_reports_url, :method => 'get' do -%>
<p>
<%= label_tag :startdate, 'Start Date Range' %>
<%= calendar_date_select_tag :startdate, params[:startdate] %>
to
<%= calendar_date_select_tag :enddate, params[:enddate] %>
</p>
<%= submit_tag 'Run Report' %>
<%- end -%>
</p>
</div>
<p></p>
<%- if params[:startdate].nil? -%>
<%- else -%>
<div id = 'results'>
<embed height="400px" width="95%" name="plugin" src="<%= formatted_report_reports_path(:pdf) %>" type="application/pdf" />
</div>
<%- end -%>
reports_controller.rb
def report
respond_to do |format|
format.html do
end
format.pdf do
pdf = make_report_pdf(params[:startdate],params[:enddate])
if pdf.nil?
else
send_file pdf, :type=> "application/pdf", :disposition => "inline"
end
end
end
end
...the params values are wiped until they're set again, and I guess that's what's happening, but how do I set them again so that the embed code will have it?
Yes, this is the case. The params
only have the lifespan of a given request.
I'm guessing a little here, but I think you need this for your path call:
formatted_report_reports_path(:startdate => params[:startdate], :format => :pdf)