简体   繁体   中英

Ruby on rails array - no implicit conversion of symbol into integer

I made a table component using react js, which uses columns to display data (which works fine with other data). For example, column 1 would show the title, column 2 the year, and column 3 the format.

Here is an example of my JSON:

{"movies": [{"title": "Iron Man", "year": "2008", "format": "DVD"}, {"title": "Iron Man 2", "year": "2010", "format": "DVD"}, {"title": "Iron Man 3", "year": "2013", "format": "DVD"}]}

Here is my code to populate the table, but it does not seem to work:

@movieList = #Makes a call to my mock API to get list of movies
@movies = Array.new

@movieList.each do |item|
 @movie = Hash.new
 @movie[:column1] = item[:title]
 @movie[:column2] = item[:year]
 @movie[:column3] = item[:format]
 @movies << @movie
end

I need some advice to overcome a "no implicit conversion of symbol into integer error" I get. Could anyone offer some advice and point out where I am going wrong?

tl;dr

use @movieList["movies"].each

explanation

The issue here, is that you act as though your @movieList is ann array , when it is actually a hash (assuming @movieList is the JSON you showed).

each works on both arrays and hashes. However, when you use it on a hash, the block is passed |key, val| . Also, assigning block variables is optional. So, when you say @movieList.each do |item| , item is actually the top level key of the hash ("movies").

Strings such as "movies" respond to [] indexing with numbers . That's why you get the error no implicit conversion of symbol into integer ... because you pass a symbol to String#[] and it expects an integer.

Another way to write this code, that is more idiomatic, would be like so:

@movies = @movieList["movies"].map do |movie|
  {
    column1: movie["title"],
    column2: movie["year"],
    column3: movie["format"]
  }
end

try reassigning

@movieList = @movieList[:movies] this will solve your problem. You're trying to iterate a object instead of an array.

lemme know if it solves your problem.

You need to loop movies using @movieList["movies"] as your JSON is a hash that has a key 'movies' and an array of movies as a value => {'movies': [{...},{...},...]}

As @max pleaner explained assigning block variables is optional, but when you use each on a hash(your JSON in this case) and provide only one block variable (instead of two refering to the keys and values of the hash), your key-value pairs are converted to two-element arrays inside the block where first element is the key and second one is the value of the pair.

Your item looks like this inside your each block - ['movies', [{movie1}, {movie2},..]] , hence:

item[0] # 'movies'
item[1] # [{movie1}, {movie2},...]

As arrays expect indexing with integers and you supply symbol ( item[:title] ), you receive:

TypeError (no implicit conversion of Symbol into Integer)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM