Can't Convert String into Integer in Ruby/Ruby-On-Rails

Ruby : TypeError: can't convert String into Integer

A small change in your code should get the right data:

@jdoc = JSON.parse(result)
@uid = @json['owner']['uid'] #=> "bb4123ac7950435eb516e2a47940b675"

Also, you're trying to convert a string: "bb4123ac7950435eb516e2a47940b675" into integer: to_i() in your code. Which would be 0 as uid is a string of alphanumeric characters, and not a combination of some random numbers/integers. I would suggest that you save that information in a varchar column instead.

Rails can't convert string into integer error

I found the fix to my problem by looking at this article: http://www.railscook.com/recipes/multiple-files-upload-with-nested-resource-using-paperclip-in-rails/

I needed to change the way I was handling multiple uploads.

Changing my input to this:

<%= file_field_tag "assets[]", type: :file, multiple: true, id: 'file-upload3' %>

and adding the following to my create and update actions in my controler:

if params[:assets]
params[:assets].each { |asset|
@listing.assets.create(asset: asset)
}
end

Doing these things resolved my issue.

Ruby pack - TypeError: can't convert String into Integer

string[x] in Ruby 1.8 gives you a Fixnum (the character code) and in 1.9 it gives you a single character string.

Array#pack turns an array into a binary sequence. The "c*" template to pack converts an array of Ruby integers into a stream of 8-bit signed words.

Here are the solutions, those comes from the Google Groups

1.

Background

>> %w(a b c).pack('*c')
TypeError: can't convert String into Integer
from (irb):1:in `pack'
from (irb):1
from /usr/bin/irb:12:in `<main>'
>> [1, 2, 3].pack('*c')
=> "\x01"
>> %w(a b c).map(&:ord).pack('*c')
=> "a"
Solution

irb(main):001:0> length=nil
=> nil
irb(main):002:0> token_string ||= ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!$:*)
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$:*"
irb(main):003:0> (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.map(&:ord).pack("c*")
=> "rd!:!LcxU3ON57*t2s520v*zvvdflSNAgU6uq14SiD00VUDlm9:4:tJz5Ri5o"
irb(main):004:0>

2.

The return type of String's [] function was Fixnum in 1.8 but is String in 1.9:

>JRUBY_OPTS=--1.9 ruby -e "puts 'a'[0].class"
String

>JRUBY_OPTS=--1.8 ruby -e "puts 'a'[0].class"
Fixnum

JRuby 1.7.x defaults to acting like Ruby 1.9.3. You need to set JRUBY_OPTS.

3.

try join instead of pack

irb(main):004:0> length=nil
=> nil
irb(main):005:0> token_string ||= ["A".."Z","a".."z","0".."9"].collect { |r| r.to_a }.join + %q(!$:*)
=> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$:*"
irb(main):006:0> token = (0..(length ? length : 60)).collect { token_string[rand( token_string.size)]}.join("c*")
=> "Fc*Dc*1c*6c*ac*Kc*Tc*Qc*Hc*jc*Ec*Kc*kc*zc*sc*3c*ic*hc*kc*wc**c*Wc*$c*Kc*Ic*Uc*Cc*bc*Pc*1c*!c*mc*Bc*lc*dc*ic*Dc*sc*Ac*Bc*nc*Kc*mc*Lc*oc*Zc*Xc*jc*6c*2c*Uc*ec*Yc*Dc*vc*Ic*Uc*5c*Zc*3c*o"
irb(main):007:0>

4.

if you're just trying to make a string of random characters that are 8-bit clean you may want to look at Random#bytes and something like Base64.encode64.

5.

active_support/secure_random also has a nice API for these things

Ruby on Rails can't convert String into Integer

I'd guess that your song_hash isn't really a Hash at all, it is an Array. Probably this array from the incoming data:

"guessed_songs":[{"song_id":1,"guessed":"YES"},{"song_id":2,"guessed":"YES"},{"song_id":3,"guessed":"YES"},{"song_id":4,"guessed":"YES"},{"song_id":5,"guessed":"YES"}],

The error you're seeing is almost always a result of trying to index an Array as though it was a Hash. For example:

>> song_hash = [ ]
>> song_hash['song_id']
TypeError: can't convert String into Integer

The only indexing in your code is:

song_hash['song_id']

and that is certainly something being indexed with a string so it matches the TypeError you're seeing. Changing that to:

song_hash['song_id'].to_i

won't help because the offending [] method will be called before to_i gets a chance to do anything.

Internal Server Error can't convert String into Integer (in Rails)

I solved the error using the old version of project and using the db with above given migrations applied on it. Then i started entering the new code of model in my project, and ran the project, the error was gone!
Thanks

can't convert String into Integer from inside hash

More data would've been more helpful than less data, but essentially your problem is this: "json" contains a string. Nothing more. Not an object. Nothing you could reference with results[]. Assuming the JSON is all properly formatted, what you would need to do is something like this:

<%= JSON.parse( result["json"] )["linked_agents"]["_resolved"]["sort_name"] %>

Now, this is horribly sloppy, and not the way you're going to want to accomplish your goals (assuming this is even effective with your data set). In actuality, what you'll want to do is parse that JSON into a variable, and work through in hash form.

It is difficult to clearly keep the topics at work here because there isn't a clear question. I would encourage you to make sure you have a basic familiarity of ruby arrays, hashes, and nested structures consisting thereof. Additionally, take a look at how you're retrieving the data that goes into results["json"].

There are many ways to accomplish many things once your data is properly structured, but you need to have a clear understanding of these types of structure before you can work magic (and you need to possess a basic knowledge of what you aren't understanding before we can provide you with solutions).

I hope that is helpful. Feel free to discuss points of confusion in the comment, and I will update this answer as we approach a solution for your problem.

Ruby Hash: can't convert String into Integer TypeError

JSON.parse(request.body.read) gives you an array of hash. So the fix is edit_id = data[0]["downloadID"]. Write p data instead of puts data, you will see the data is an array of hash.

Can't convert String into integer in ruby/ruby-on-rails

Apparently there was nothing wrong with the code. It was probably related to my browsers cache or something of that nature, as it started working the next day. Sorry for the inconvenience.

Ruby on rails: can't convert String into Integer for if contition

q is probably an instance of Array, not Hash, so q.[] method expects integer argument.



Related Topics



Leave a reply



Submit