JackHasaKeyboard
JackHasaKeyboard

Reputation: 1685

Ruby displaying "InvalidURIError" when certain string is put through regex

I'm attempting to grab the URL of every image within a directory and display them with image_tag(s), but Ruby was appending "images/" in front of the image src and made them not work.

I tried to rectify this by removing everything before the name of the subdirectory in the images folder but it caused an error and only with that one specific word.

Using HAML

@models = Dir.glob("app/assets/images/creation/model/*.png")
@i = 0;

- @models.each do |model|
      - @i++
        %td{:id => "#{@i}"}
          %img= image_tag model.gsub!(/.*?(?=creation)/im, "")

Inserting "creation" breaks it, inserting seemingly anything else is fine.

Was there something I did wrong?


Update: I tried renaming the folder and changing the code accordingly, same thing happened. Inserting the name of the folder with one character changed doesn't display an error and renders nothing, but as soon as you put in the actual name you get "InvalidURIError"

Upvotes: 0

Views: 84

Answers (2)

Aleksey Shein
Aleksey Shein

Reputation: 7482

Use this code:

%img= image_tag URI.encode("creation/model/#{model.split('/').last}")

Upvotes: 0

JackHasaKeyboard
JackHasaKeyboard

Reputation: 1685

Upon taking a look at the error I realized that Rails was taking issue with a single image's filename, I guess it had characters in it that Rails didn't like.

Weird, I've used the same image in a PHP application fine.

Upvotes: 1

Related Questions