I'm having an encoding problems with Portuguese characters. It didn't render correctly.
This is replacing the Portuguese characters by ���
In my email method I have something like:
#html_content = html_content
#text_content = text_content
mail(
:from => "#{from_name} <notifications#email.com>",
:to => options[:recipients],
:subject => options[:subject]
)
and in my views I have something like that:
<%= #html_content %>
I already checked the variable content and everything looks good. So probably the problem happens inside the method mail(...)
I tried to follow this instructions
actionmailer encoding - rendering garbage in email client
and didn't work. Any idea?
I'm using Rails 3.2.2 and Ruby 1.9.3
[UPDATE]
The bytes values are:
[208, 146, 209, 150, 209, 130, 208, 176, 208, 187, 209, 150, 208, 185]
[80, 111, 114, 116, 117, 103, 117, 195, 170, 115]
The message content is:
Віталій
Português
Related
I'm trying to replace big % numbers to shorters versions (10000% -> 10k%). Generally code works, but if number_to_percentage used it stop working (with TOTALY SAME string).
Loading development environment (Rails 5.1.2)
2.3.1 :001 > "10000.000%".bytes
=> [49, 48, 48, 48, 48, 46, 48, 48, 48, 37]
2.3.1 :002 > helper.number_to_percentage(10000).bytes
=> [49, 48, 48, 48, 48, 46, 48, 48, 48, 37] # totally same
2.3.1 :003 > helper.number_to_percentage(10000).sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "k%" # doesn't work
2.3.1 :004 > "10000.000%".sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "10k%" # works
What can cause this? Any ideas?
Because number_to_percentage returns an ActiveSupport::SafeBuffer and not a String.
helper.number_to_percentage(10000).class # => ActiveSupport::SafeBuffer
ActiveSupport::SafeBuffer (which is a subclass of String) does some magic around unsafe methods like sub. That's why you can have some surprises.
The key difference is:
"10000.000%".class #=> String
number_to_percentage(10000).class # => ActiveSupport::SafeBuffer
ActiveSupport::SafeBuffer is a subclass of String, and contains the concept of UNSAFE_STRING_METHODS (including sub and gsub). This concept is useful for rails views (which is where number_to_percentage is normally used!), in relation to security; preventing XSS vulnerabilities.
A workaround would be to explicitly convert the variable to a String:
number_to_percentage(10000).to_str.sub(/(\d\d)\d\d\d(?:[.,]\d+)?\%$/){ "#{$1}k%" }
=> "10k%"
(Note that it's to_str, not to_s! to_s just returns self, i.e. an instance of ActiveSupport::SafeBuffer; whereas to_str returns a regular String.)
This article, and this rails issue go into more detail on the issue.
Alternatively, you could write your code like this, and it works as expected:
number_to_percentage(10000).sub(/(\d\d)\d\d\d(?:[.,]\d+)?%$/, '\1k%')
#=> "10k%"
I would actually prefer this approach, since you are no longer relying on modification to the (non-threadsafe) global variable.
I'm trying to run codes below, got from https://github.com/a2800276/8583, I put it in my /lib then try to call the BerlinMessage to test if the code was working then the error prompted. Im wondering why I unitialized constant. Thanks.
BerlinMessage.rb
require 'iso8583'
module ISO8583
class BerlinMessage < Message
mti_format N, :length => 4
mti 1100, "Authorization Request Acquirer Gateway"
mti 1110, "Authorization Request Response Issuer Gateway"
mti 1420, "Reversal Advice Acquirer Gateway"
mti 1421, "Reversal Advice Repeat Acquirer Gateway"
mti 1430, "Reversal Advice Response Issuer Gateway"
mti 1804, "Network Management Request Acquirer Gateway or Issuer Gateway"
mti 1814, "Network Management Request Response Issuer Gateway or Acquirer Gateway"
bmp 2, "Primary Account Number (PAN)", LLVAR_N, :max => 19
bmp 3, "Processing Code", N, :length => 6
bmp 4, "Amount (Transaction)", N, :length => 12
bmp 6, "Amount, Cardholder Billing" , N, :length => 12
bmp 7, "Date and Time, Transmission" , MMDDhhmmss
bmp 10, "Conversion Rate, Cardholder Billing", N, :length => 8
bmp 11, "System Trace Audit Number (STAN)", N, :length => 6
bmp 12, "Date and Time, Local Transaction", YYMMDDhhmmss
bmp 14, "Date, Expiration", YYMM
bmp 22, "POS Data Code", AN, :length => 12
bmp 23, "Card Sequence Number", N, :length => 3
bmp 24, "Function Code", N, :length => 3
bmp 25, "Message Reason Code", N, :length => 4
bmp 26, "Card Acceptor Business Code", N, :length => 4
bmp 30, "Amounts, Original", N, :length => 24
bmp 32, "Acquiring Institution Identification Code", LLVAR_N, :max => 11
bmp 35, "Track 2 Data", LLVAR_Z, :max => 37
bmp 37, "Retrieval Reference Number", ANP, :length => 12
bmp 38, "Approval Code", ANP, :length => 6
bmp 39, "Action Code", N, :length => 3
bmp 41, "Card Acceptor Terminal Identification", ANS, :length => 8
bmp 42, "Card Acceptor Identification Code", ANS, :length => 15
bmp 43, "Card Acceptor Name/Location", LLVAR_ANS, :max => 56
bmp 49, "Currency Code, Transaction", N, :length => 3
bmp 51, "Currency Code, Cardholder Billing", N, :length => 3
bmp 52, "Personal Identification Number (PIN) Data", B, :length => 8
bmp 53, "Security Related Control Information", LLVAR_B, :max => 48
bmp 54, "Amounts, Additional", LLLVAR_ANS,:max => 40
bmp 55, "Integrated Circuit Card (ICC) System Related Data", LLLVAR_B, :max => 255
bmp 56, "Original Data Elements", LLVAR_N, :max => 35
bmp 58, "Authorizing Agent Institution Identification Code", LLVAR_N, :max => 11
bmp 59, "Additional Data - Private", LLLVAR_ANS, :max => 67
bmp 64, "Message Authentication Code (MAC) Field", B, :length => 8
bmp_alias 2, :pan
bmp_alias 3, :proc_code
bmp_alias 4, :amount
bmp_alias 12, :exp_date
end
end
if __FILE__==$0
mes = ISO8583::BerlinMessage.new
mes.mti = 1110
mes[2] = 474747474747
mes["Processing Code"] = "123456"
pan = mes["Primary Account Number (PAN)"]
#mes.pan = 47474747474747
#puts mes.pan
puts mes.to_b
puts mes.to_s
#mes2 = BerlinMessage.parse input
end
After running the code I got this error,
Thanks.
To cite from the comment in the code you linked to explaining the mti_format method:
# the subtyped message should be told how the MTI is encoded:
#
# class MyMessage < Message
# mti_format N, :length => 4
# (...)
# end
#
# `N` above is an instance of Field which encodes numbers into their
# ASCII representations in a fixed length field. The option `length=>4`
# indicates the length of the fixed field.
Thus, you need to replace the N with the class name of your field implementation. As it stands now, ruby interprets your literal N as the name of a constant in the current class and tries to resolve it to a value. As this doesn't work, you get the error you have received.
I am trying to setup grape api. While I'm including defaults module:
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
version 'v1'
format :json
end
end
end
end
the error undefined method ` ' for API::V1::Projects:Class occurs. Also, when I paste
version 'v1'
format :json
to classes without doing mixin, it works. My operating system is Mac OS
The format :json line is indented by both, normal spaces and en spaces.
Inspecting the line's code points reveals them:
line = " format :json"
line.codepoints
#=> [8194, 32, 8194, 32, 8194, 32, 8194, 32, 102, 111, 114, 109, 97, 116, 32, 58, 106, 115, 111, 110]
# ^ ^ ^ ^
Replacing the en spaces with normal spaces should fix the problem.
Currently I am upgrading my Rails app from rails 3 to rails 4 and i am using spreadsheet(0.7.1)
after running server I got undefined method `send_file' for main::object
this is my code:
def financial_export_excel
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
sheet.merge_cells(5, 0, 6, 0)
sheet.merge_cells(5, 1, 6, 1)
sheet.merge_cells(5, 2, 6, 2)
sheet.merge_cells(5, 3, 6, 3)
sheet.merge_cells(5, 4, 6, 4)
sheet.merge_cells(5, 5, 6, 5)
sheet.merge_cells(5, 6, 6, 6)
sheet.merge_cells(5, 7, 6, 7)
sheet.merge_cells(5, 8, 6, 8)
book.write "sample.xls"
send_file "sample.xls"
File.delete "sample.xls"
end
any help on this?
It seems your are storing the file in location and send it for download and finally delete it.
Since you deleted the file you con't get it for download. If you remove the line
File.delete "sample.xls"
it will work.
or you can use send_data method instead of send_file method.
eg:
send_data book, :filename => "sample.xls", :type =>
"application/vnd.ms-excel"
reference
send_data
I have a variable number of tables with variable number of rows and I want to have them displaying one after the other but if a table doesn't fit on the current page put it on the next then continue on. I have put the table in a transaction so I can roll back then print it if the height will fit on curent page but how do I get the table height?
I have this code at the moment
pdf.transaction do
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.move_down 20
#pdf.rollback
end
Any help on this would be great. Or any other way to do this ?
Best Regards
Rick
4 years later... :)
As #m-x wrote it, rollback was disabled for security reason, like "group", and is still not implemented. So, here how I deal with break pages for tables :
Big and simple table (one row per data)
Just use header option
pdf.table #data,
header: true, # You can use 'header: 2' if your header take two rows
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
Small table or complexe table
make table
check if you need break page
draw table
With your example :
t = pdf.make_table #data,
font_size: 12,
border_style: :grid,
horizontal_padding: 10,
vertical_padding: 3,
border_width: 2,
position: :left,
row_colors: ["FFFFFF","DDDDDD"]
if cursor - t.height < 0
start_new_page
end
t.draw
Hope that helps
#current_page = pdf.page_count
#roll = pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
pdf.rollback if pdf.page_count > #current_page
end
if #roll == false
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
I hope this works for you as for me :-)
I'm a Prawn beginner, so this might not be the best solution, but it should work.
You can get the table height if you consider the font size and vertical padding and the number of records you have in #data and you can get the current cursor position by calling Prawn::Document.cursor method.
Having these two numbers you should be able to check whether the table fits on this page or not. If not, just start a new one (by calling Prawn::Document.start_new_page method).
Otherwise the table will break automatically and will continue on the next page.
Thanks Igor
I am currently setting the current page and then in the transaction after the new table has been rendered and before the roll back setting new_page variable. Then i can roll back and chek if the new page var > current page var and if it is start new page and print the table. See code below.
The problem is now the pdf.start_new_page says error but if i just take the pdf.rollback line out it works. See error below.
Any ideas any one or any easier ways, there must be one!!
thanks
rick
#current_page = pdf.page_count
pdf.transaction do
pdf.move_down 20
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
#the_next_page = pdf.page_count
pdf.rollback
end
if #the_next_page > #current_page
pdf.start_new_page
pdf.table #data,
:font_size => 12,
:border_style => :grid,
:horizontal_padding => 10,
:vertical_padding => 3,
:border_width => 2,
:position => :left,
:row_colors => ["FFFFFF","DDDDDD"]
end
The error
> You have a nil object when you didn't expect it!
The error occurred while evaluating nil.identifier
Extracted source (around line #158):
155: end
RAILS_ROOT: C:/InstantRails/rails_apps/Macrotec-Application
Application Trace | Framework Trace | Full Trace
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:302:in `go_to_page'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:128:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `each'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document/internals.rb:127:in `finalize_all_page_contents'
c:/InstantRails/ruby/lib/ruby/gems/1.8/gems/prawn-core-0.7.1/lib/prawn/document.rb:344:in `render'
C:/InstantRails/rails_apps/Macrotec-Application/app/views/quotations/show.pdf.prawn:158:in `_run_prawn_app47views47quotations47show46pdf46prawn'