I am attempting to export my data to a CSV file. This is my model:
def self.generate_csv(ids)
CSV.generate({:col_sep => "\t"}) do |csv|
csv << ['topfind terminus id','position','sequence','protein (uniprot ac)','topfind evidence ids']
ids.each do |id|
n = Nterm.find(id)
csv << [n.externalid,n.pos,n.protein.sequence[n.pos-1..n.pos+9],n.protein.ac,n.evidences.collect{|e| e.externalid}.join(':')]
end
end
end
And this is the controller for this model that calls this method:
def show
puts "id search: [#{params[:id]}]"
@output = Nterm.generate_csv(params[:id])
respond_to do |format|
format.html
format.csv { send_data @output.as_csv }
end
p @output
end
And this is my view:
<%= link_to "Export to CSV", {:method=> "show", :id => @nterm.map {|i| i.id }} %>
I'm not quite sure what to put on my show view in this case. How do I produce a CSV?
Change your view as following, use your route path for view action, instead of id
, pass ids
because its collection of ids not single id.
<%= link_to "Export to CSV", your_route_path(:format => :csv, :ids => @nterm.map {|i| i.id }}) %>
And slide changes need to your action as following, for receive ids
def show
@output = Nterm.generate_csv(params[:ids])
#your codes goes here
end