Home > Enterprise >  Render html using csv data rails
Render html using csv data rails

Time:12-01

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!

CodePudding user response:

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