Achaius
Achaius

Reputation: 6124

How to get all files in image directory Rails4 in array

I have migrated from Rails3 to Rails4. The following code returns Array in Rails3 but in Rails4 it returns string with illegal character.

Dir.glob("app/assets/images/flowers/*")

sample output in Rails3

["app/assets/images/flowers/rose.png", "app/assets/images/flowers/lilly.png"]

output in Rails4

"\x04\b[dI\"8app/assets/images/flowers/rose.png\x06:\x06ETI\"4app/assets/images/flowers/lilly.png"

How to get same output format as in Rails3?

Upvotes: 1

Views: 1349

Answers (2)

Litmus
Litmus

Reputation: 10996

try this

 files = Dir.glob("app/assets/images/flowers/*").map do |f| File.basename f end

Upvotes: 1

Ruslan
Ruslan

Reputation: 1301

Dir has nothing to do with Rails — it's pure Ruby class. Here is the API reference to it. According to API it should always return an Array. My guess is that you messed up something in your Ruby installation while you were upgrading Rails 3 to 4.

I think best bet will be a clean installation of ruby/rails. You could also try to run Dir.glob() from both IRB and rails console to see where the mistake happens; and start from there.

Upvotes: 0

Related Questions