Rails3+CSV wrong number of arguments (0 for 1) , in generate - ruby-on-rails

In development mode iam using (rails3 and ruby 1.9.2p136) and CSV is working good,
but,
In production mode iam using (rails3 and ruby 1.9.2p0) and CSV throws error
(wrong number of arguments (0 for 1) , in generate)
please can u suggest me what could be the problem.
thanks

I encountered exact same problem on Fedora15 with ruby 1.8.7 .
Code below fixed the CSV.generate problem.
if RUBY_VERSION < "1.9"
require "rubygems"
require "faster_csv"
CSV = FCSV
else
require "csv"
end

Related

Rails 5 cipher.key "key must be 32 bytes" error

Brand new Rails application.
Rails version 5.0.0.1, Ruby version 2.4.0preview2.
Create application "demo", run a simple scaffold generate Product, and get an error when trying to view the scaffold's overview page (base index file still loads the Welcome to Rails screen fine):
ArgumentError in ProductsController#index
key must be 32 bytes:
cipher = new_cipher
cipher.encrypt
cipher.key = #secret
# Rely on OpenSSL for the initialization vector
iv = cipher.random_iv
The problem line is apparently cipher.key = #secret.
I've seen various mentions on the github repo for Rails mentioning this issue, but all implied it was now resolved in Rails 5.0.0.1
Ok, there was a slight misunderstanding on my part, looks like the fix is coming in 5.0.1 not 5.0.0.1
https://github.com/rails/rails/issues/26694
Please use Digest::MD5 to achive 32 bytes
require 'openssl'
require 'digest'
require 'base64'
data = "encrypt me"
secret_key = "asd3dssdf34HDas"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = Digest::MD5.hexdigest(secret_key) # this will convert key length into 32
encrypted_data = c.update(data.to_s) + c.final
encrypted_data = Base64.urlsafe_encode64(encrypted_data, padding: false) #padding: false will remove '/', '+' from encrypted data
encrypted_data.gsub! "\n",""
Or Simply use secret key of length 32 bytes
data = "encrypt me"
secret_key = "Aswertyuioasdfghjkqwertyuiqwerty"
c = OpenSSL::Cipher.new("aes-256-cbc")
c.encrypt
c.key = secret_key
encrypted_data = c.update(data.to_s) + c.final
Finally found problem! It was from a bugfix... https://bugs.ruby-lang.org/issues/12561
If you are using cipher e.g. 'aes-256-cfb', the key_len is 32, found by:
require 'openssl'
cipher = OpenSSL::Cipher.new('aes-256-cfb')
cipher.key_len # => 32
We had mistakenly thought we needed to send a 256 character nonce, but actually you are supposed to send a 32 character nonce - or
use cipher.random_key (which internally uses the key_len). It never used to be a problem because openssl truncated the nonce... but now you need to send the right lengthed nonce.
We got this error upgrading ruby from 2.3.4 to 2.4.2.
This issue turns out to be connected to the key you are using. Without changing your key you can use the code below to transform your key to 32 bytes:
attr_encrypted :attribute, key: ENV['MY_KEY'].bytes[0..31].pack( "c" * 32 )
try this:
rake db:create
rake db:migrate
then, the most important thing:
bundle update
This works for me.
Use random_key so it always fit.
key = cipher.random_key
cipher.key = key
reference http://ruby-doc.org/stdlib-2.0.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html
Solution:
Edit your Gemfile
Add the following line: gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
bundle install
Optional: I am using ruby2-4.1 . (rvm install ruby-2.4.1)
Rational: The rails version prior to 5.0.0 seems to have a bug that causes this issue. The bug has been resolved in the latest version of Rails. If you hare following the Rails Installation Guide (http://railsapps.github.io/installrubyonrails-mac.html) you will probably encounter this problem as of this posting date.
This fix does work, and is verified by
I was having this problem too and fixed it by running
bundle update
Make sure that you have the latest version of rails installed.
Had the same error:
Running bundle update should do the trick

Read GB2312 encoding page using Ruby

I am trying to parse GB2312 encoded page (http://news.qq.com/a/20140824/015032.htm), and this is my code.
I am not yet into the parsing part, just in the open and read, and I got error.
This is my code:
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read
And this is the error:
Encoding::InvalidByteSequenceError: "\x8B" on GB2312
I am using Ruby 2.0.0p247
Any solution?
I don't know exactly why this happens when calling .read, but you can work around it if you are using Nokogiri. Just pass the file object directly to Nokogiri without calling .read:
require 'open-uri'
file = open("http://news.qq.com/a/20140824/015032.htm")
document = Nokogiri(file)
I cannot duplicate the error using 2.0.0p247,
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read
Works fine.
However
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read.encode('utf-8')
will raise the error
Encoding::InvalidByteSequenceError: "\x8B" on GB2312
Are you trying to do some encoding conversion?
you can try this
document = Nokogiri::HTML(open("http://news.qq.com/a/20140824/015032.htm"), nil, "GB18030")

uninitialized constant Spreadsheet::Link - roo gem - xlsx file

When I open a file in xlsx format, using empact/roo gem, this line of code:
data = Roo::Spreadsheet.open("/Users/asd/Desktop/in_xlsx.xlsx", extensions: :xlsx)
or this line
data = Roo::Excelx.new("/Users/asd/Desktop/in_xlsx.xlsx")
works perfect! (at least this is what I think)
data is now a Roo::Excelx object with the columns and rows filled correctly.
But whenever I try to use a method like data.first_row or data.cell(1,1), I get this
NameError: uninitialized constant Spreadsheet::Link
from /Users/asd/.rvm/gems/ruby-2.0.0-p353#ch/gems/roo-1.13.2/lib/roo/excelx.rb:379:in `set_cell_values'
Additional info:
MacOS 10.9.1
Rails 4.0.2
Ruby 2.0.0-p353
Roo (1.13.2)
Any help is really appreciated!
Try this :
require 'rubygems'
require 'roo'
For more information http://roo.rubyforge.org/

Name Error in Excel Uninitalized Contant in 'roo'

I am trying to read an Excel file in Ruby On Rails.
I have done coding like this for reading the cell content from the Excel sheet.
def test
require 'rubygems'
require 'iconv'
require 'roo'
s = Excel.new("C:/Sites/hmmsapp/Book1.xls")
s.default_sheet = s.sheets.first
1.upto(4) do |line|
roll = s.cell(line,'A')
puts "#{roll} -------------"
end
end
But on running this it always gives me this error.
NameError in HostelController#test
uninitialized constant HostelController::Excel
I have also included iconv as per suggestions for this problem. But there is no change in error.
Please give some light to removing this error & to read the excel file properly.
Try Roo::Excel.new
Or Roo::Spreadsheet.new

Fastercsv shows malformedCSVError, what am i doing wrong?

I am implementing in Ruby and i am running a project which reads a CSV file to add users.
but when i pick my file it just gives always the same error:
FasterCSV::MalformedCSVError in User importController#match
Illegal quoting on line 1.
my CSV file just exists of :
"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0
anyone who knows what can be wrong?
Try to upgrade your FasterCSV gem version. With the latest version it works:
FasterCSV.parse_line '"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0'
=> ["RubenPersoon1", "test", "Bauwens", "Ruben", "rub#gmail.com", "0"]
ruby-1.8.7-p352 :005 > FasterCSV.parse '"RubenPersoon1","test","Bauwens","Ruben","rub#gmail.com",0'
=> [["RubenPersoon1", "test", "Bauwens", "Ruben", "rub#gmail.com", "0"]]
Also, keep in mind that if you are on Ruby 1.9.2, FasterCSV is already included. Just require 'csv' and use the CSV class.

Resources