When I generate a csv file in Ruby, the Ruby CSV library is separating out the numbers based on its thousands comma. For example numbers such $5.00 stay in their own cell, while numbers like $257,000,415.81 will instead be surrounded in double quotes and get split up on the thousands comma into the next cell. example ("$257 | 000 | 415.81"). The csv parses the number correctly as long as it is not the first value. How do I get the csv library to properly format the number in the same cell? The program i'm using to open the csv file is Microsoft Excel.
Here is a screen shot of how the text looks in excel.
http://i.stack.imgur.com/8EMs0.png
And here is a screen shot of how I would like the text to look.
http://i.stack.imgur.com/opjrR.png
Below is the code for the csv.
<%= CSV.generate_line(["Balance"]).strip %>
<% #accounts.each do |acct| %>
<% row = [
(number_to_currency acct.balance, :unit => "$")
] %>
<%= CSV.generate_line(row).strip.html_safe %>
<% end %>
This should work to create the CSV file correctly:
CSV.open('filename.csv', 'w') do |csv|
#accounts.each do |acct|
csv << [ number_to_currency(acct.balance, unit: '$') ]
end
end
Related
I just want to make a simple each loop in my Middleman helper, datas are stored in my page'Frontmatter like this :
dir:
- test
- test2
So in my helper, I try to write my loop :
def translate_directory
current_page.data.dir.each do |dir|
dir
end
end
call my method in my page
<%= translate_directory %>
and this is what's display :
["test", "test2"]
But now, if I make the same loop in my page, write with ERB syntax :
<% current_page.data.dir.each do |x| %>
<%= x %>
<% end %>
the exit is the following
test test2
separated in two strings, so exactly what I want.
EDIT : when I puts the helper'method, it display the two strings in two lines, so in two separated strings. Don't understand why it appear as an array on my browser.
EDIT 2 : a little thing I forgot, I want to translate each word with I18n.translate, like this :
def path_translate
current_page.data.dir.each { |dir| t("paths.#{dir}", locale: lang) }
end
but i can't because the each method doesn't work so I18n can't translate each word.
Because your helper is returning an array not a interpolated string like the ERB template is doing. Try the following for your helper:
def translate_directory
current_page.data.dir.join(' ')
end
My bad. Using .map instead of .each fix the problem, then use .join makes the array a big string.
How do I display data from a csv file into a Sinatra-App?
Sinatra:
csv = CSV.read(data.csv)
csv.each do |entry|
#output = "#{entry[1]},#{entry[4]}:#{entry[0]}"
end
Erb:
<%= #data %>
Which displays only the last row of the csv file.
Problem
With this code :
csv = CSV.read(data.csv)
csv.each do |entry|
#output = "#{entry[1]},#{entry[4]}:#{entry[0]}"
end
You iterate over all csv rows.
For each row, the block defines the #output variable.
After the first csv line, the code just keeps on overriding the #output_variable.
Possible solution
You need map, not each.
csv = CSV.read(data.csv)
#data = csv.map do |entry|
"#{entry[1]},#{entry[4]}:#{entry[0]}"
end
You can then use
<%= #data %>
in your views. #data is now an array of strings, with one string for each csv row.
You probably want to move the for loop to the template file.
In sinatra
#csv_data = CSV.read(data.csv)
In erb
<% #csv_data.each do |entry| %>
<%= entry[1] %>,<%= entry[4] %>:<%= entry[0] %>
<% end %>
I'm working on an application that creates random sentences. I have it working as a console application, and want to make a Sinatra app which lets me display the sentences on the browser.
I have a variable #grammar that is populated from a form. I want to pass this into a method a few methods which work together to take in a string and generate a random sentence from it using a lot of logic. My rsg.erb file looks like this.
Where 'The waves portend like big yellow flowers tonight.' is the output of the expand method. I would like to display this on the erb file so it is displayed on the browser.
How can I do that?
Can you try this:
<%= #grammar %>
<%-# Assigning values to the variables in first step %>
<%-
rds = read_grammar_defs(#grammar) #get text from file and parse
sds = rds.map { |rd| split_definition rd} #use split definition to make array of strings
tgh = to_grammar_hash(sds) #create hash
rs = expand(tgh) #create sentence
%>
<%-# Printing it in second step %>
<%= rs %>
I have a csv file with about 30 columns that i would like to output, and would like these to appear in my .csv.erb on different lines, for example:
<%= quantity.line_number %>,
<%= quantity.created_at.strftime("%Y-%m-%d %H:%M:%S") %>,
<%= quantity.partner_entity_no %>,
<%= quantity.partner_name %>,
However when I execute this then my .csv file has line breaks after every ',' I would like to 1) keep each column on separate line in source and 2) have non line breaks in the output .csv file. How could I make this happen?
Use the stdlib CSV class to get csv quoting and escaping semantics right. Or more conveniently, use its Array#to_csv helper method.
<% require 'csv' %>
...
<%= [
quantity.line_number,
quantity.created_at.strftime("%Y-%m-%d %H:%M:%S"),
quantity.partner_entity_no,
quantity.partner_name,
].to_csv %>
<%= [quantity.line_number,
quantity.created_at.strftime("%Y-%m-%d %H:%M:%S"),
quantity.partner_entity_no,
quantity.partner_name].join(',') %>
I am coding a web application in ruby on rails.
I have a set of text boxes in each one there is a character and i want to glue all these text boxes in order to make one word.
The text boxes are like this :
1 %>
any ideas ??
Well for starters dont use a for loop, they ugly.
Second I wouldn't use the text_field helper rather the text_field tag
<% (1..10).each do |n| %>
<%= text_field_tag "password[#{n}]" %>
<% end %>
That will return the password all nicely chunked up