Have a really basic question about rails – spent hours looking on the internet for a solution – but can not find a solution.
I just need to call a method on the clicking of a link.
The idea is I need to export an xml file that is generated by a method in the controller.
firms_controller.rb (exerpt)
.....def xml
@entries = Entry.find(:all)
send_data @entries.to_xml,
:type => 'text/xml; charset=UTF-8;',
:disposition => "attachment; filename=entries.xml"
end....
I just need to call this method from the firms view page to get the xml file to download, but cannot manage to get it to work. The XML code does work – if I add it to the index method in the controller it works flawlessly.
This is some of the ways I have attempted to get the link working after reading some online articles.
<% url = url_for({:action => "xml", :controller => 'firms'}.merge(params)) %>
<%= link_to "XML", url %>
The most basic way tried
<%= link_to "XML", :action => 'xml' %>
It seems to me like something that should be very simple – but cannot get it to work.
The error I am getting:
ActiveRecord::RecordNotFound in FirmsController#show
Couldn't find Firm with ID=xml
Rails.root: /home/james/rails/KPSmart
Application Trace | Framework Trace | Full Trace
app/controllers/firms_controller.rb:25:in `show'
show method in firms_controller
def show
@firm = Firm.find(params[:id])
end
Have tried messing with the routes to get it to work with no avail.
Thanks for your help
Try This
In routes write this above your resource we written for firms controller
and link this in following way