almo
almo

Reputation: 6387

link_to path(:xml) with parameters in Rails 5

I have a controller that get a start and end date from URL parameters (GET) and the get all Rides within this time range and outputs them in my view template.

@from = params[:from].to_date.beginning_of_day
@until = params[:until].to_date.end_of_day
@rides = current_user.rides.where(:date => @from..@until)

When I render this as HTML no problem. But I also want to be able to render as xml, I have written my xml.builder template but having problems passing the date range parameters.

Right now my link_to looks like this:

<%= link_to "Export", report_rides_path(:xml) %>

And in my controller I have:

respond_to do |format|
      format.html
      format.xml { send_data(render_to_string(:template=>"report/rides" ), :type=>"text/xml",:filename => "export.xml") }
end

Is there any way I can send the parameters in my link_to?

Upvotes: 1

Views: 309

Answers (1)

siegy22
siegy22

Reputation: 4413

There's a reserved option called "format", which you should use:

<%= link_to "Export", report_rides_path(format: :xml) %>

which will generate something like /reports/rides.xml. (Don't know what you're routes are)

Upvotes: 0

Related Questions