I have the following csv file data.csv:
id,name,description
1,John Doe,Lorem ipsum
I want to render this into HTML, something like:
<%= data.name + ": " + data.description %>
How can I do it? Thank you!
You should take a look at the ruby library CSV: https://ruby-doc.org/stdlib-2.6.1/libdoc/csv/rdoc/CSV.html.
You can do something along the lines this in your controller:
#rows = CSV.read('path_to_file/data.csv', headers: true)
And then look through the rows in your partial and output the data based on your header in the file:
data.each do |row|
<%= "#{row['name']}: #{row['description']}" %>
end
Related
When a click on the 'View CSV' link on my site the csv downloads but the content of the csv is in Chinese instead of English. How do I ensure the language of what I'm passing through the send_data function doesn't change? Thanks for your help!
I have the following function in my controller:
def get_data_visit
#csv_string = CSV.generate do |csv|
csv << ["row", "of", "CSV", "data"]
end
send_data Iconv.conv('iso-8859-1//IGNORE', 'utf-8', #csv_string), filename: "something.csv"
end
And the following function in my view:
<%= link_to "View CSV", get_data_visit_admin_stats_path %>
Ie. ["row", "of", "CSV", "data"] becomes 潲ⱷ景䌬噓搬瑡 and I would like it to stay in English.
I had to ensure libre office was reading utf-8 (changed in a drop down), then it worked.
I have a rails project which is exporting some csv data into excel. On some instances excel is outputting special characters.
ex.
test 1 & test 2 & test 2
reads in excel as
test 1 & test 2 & test 2Â
My default CSV encoding is set to UTF-8 and I have played around with a number of other encoding settings although none of them have seemed to solve this issue.
Here is where the csv gets generated.
<% headers = default_headers %>
<%= CSV.generate_line(headers).strip %>
<% #activities.each do |activity| %>
<% has_permission = #_controller.is_activity_permissioned_to_user?(activity, current_user) %>
<% row = activity_generate_csv_row_data(activity, headers, has_permission, preferences) %>
<%= CSV.generate_line(row).gsub(/\A""/,'').strip.html_safe %>
<% end %>
and in my controller.
format.csv {
headers['Content-Disposition'] = "attachment; filename=\"
{report_name}.csv\""
headers['Content-Type'] ||= 'text/csv'
}
Every solution i've tried has failed. I really just can't figure out how to fix this.
Try to concatenate a Byte Order Mark string with your CSV string, like:
BOM = "\uFEFF"
def some_csv_string_generating_func
...
return BOM + a_csv_string
end
This will make Excel show the CSV file correctly.
Also, I would advice against having all the CSV generation logic/code on the view, but on a helper class/module or the like.
Currently in my create.csv.erb template I have this.
<%- event_headers = #event_filters -%>
<%= CSV.generate_line(event_headers) %>
It generates a CSV file like this
event1,event2,event3,event4
I would like to add some sort of Key or header, so that the output looks like this
Events: event1,event2,event3,event4
or like this
Events:
event1,event2,event3,event4
is this possible?
Use string interpolation to construct string you wish to have :)
<%= "Events: #{CSV.generate_line(event_headers)}" %>
I'm completely newbie with RoR. What I'm trying to do is to import data from an asp url:
I've a ticket that passed to this url it returns with some informations like time_created, time_updated, owner, notes. I've tried putting this code in my view:
<% file = open("http://xxx.xxx.xxx/utility/ticket_dettagli.asp?TT="+#Ticket.ticket) %>
<%= CSV.new(file, col_sep: ',').readlines do |row| %>
<%= Ticket.create! row.to_hash %>
<% end %>
without success...any suggestion?
Why are you trying to do this in view? I think, there should be rake task for import tickets form csv or class method in your model ...
See: http://railscasts.com/episodes/396-importing-csv-and-excel
#Ticket - is this a variable? How you declare it?
I want to give my users the ability to export a table to CSV.
So in my controller, I've added on top of the file:
respond_to :html, :js, :csv
I'm also setting the headers if the requested format is csv:
if params[:format] == 'csv'
generate_csv_headers("negotiations-#{Time.now.strftime("%Y%m%d")}")
end
Code for generate_csv_headers(in application_controller) is:
def generate_csv_headers(filename)
headers.merge!({
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Type' => 'text/csv',
'Content-Disposition' => "attachment; filename=\"#{filename}\"",
'Content-Transfer-Encoding' => 'binary'
})
end
I've also created a view named index.csv.erb to generate my file:
<%- headers = ["Id", "Name"] -%>
<%= CSV.generate_line headers %>
<%- #negotiations.each do |n| -%>
<%- row = [ n.id,
n.name ] -%>
<%= CSV.generate_line row %>
<%- end -%>
I don't have any error, but it simply displays the content of the CSV file, while I'd expect a prompt from the browser to download the file.
I've read a lot, but could not find anything that'd work.
Do you have an idea?
thanks, p.
I'm still unsure about why this fixed the issue, but it did.
I changed the link in the view to
<%= link_to "Export to csv", request.parameters.merge({:format => :csv})%>
and it now works!
I'm not sure you have to do it this way, but if you generate and save the file, you can use send_file to send it to the browser. See http://api.rubyonrails.org/classes/ActionController/Streaming.html
You might be interested in this gem I made called CSV shaper that allows you to create CSV output using a really nice Ruby DSL.
It will also handle setting the response headers correctly, while allowing filename customisaton.
https://github.com/paulspringett/csv_shaper