Question

I really don't know how to ask for what I'm asking for ... I'd google if I knew what I was looking for!

So, I have a set of carrierwave versions of images names image00, image01, image02 etc up to image25 stored as:

@photo.main.image00, @photo.main.image01 etc ...

I want to be able to use a controller to post these images out. But I have no idea how to string the request together. I've got objective-c, javascript, Ruby and a host of other languages fighting for space in my head, and Ruby seems to have lost out!

I wish to be able to make a call such as:

@photopiece = Base64.strict_encode64(File.read(@photos.main.imageXX.current_path))

but have no idea how to replace the XX with a digit and still have the file encoded.

I tried:

imagestring = "@photos.main.image#{sendPiece}.current_path"

and then

@photopiece = Base64.strict_encode(File.read(imagestring))

But that did nothing ... I could almost hear the compiler laughing at me (such is my paranoia now!)

So, how do I get the photo names to appear in the form:

@photos.main.imageXX.current_path

such that they can get parsed by strict_encode?

Thanks in advance for help! And reading my ramblings ...

PS. Apologies for the really bad heading! Please change it if you know what I'm asking for!

Was it helpful?

Solution 2

Maybe, it would be more secure than silly eval(params..):

 number_is_safe = !!Float(sendPiece) rescue false
 if number_is_safe
   path = eval "@photos.main.image#{sendPiece}.current_path"
   @photopiece = Base64.strict_encode(File.read(path))
 end

jvnill propose much better solution:

piece = @photos.main.public_send("image#{sendPiece}")
@photopiece = Base64.strict_encode64(File.read(piece.current_path))

OTHER TIPS

You can use public_send

piece = @photos.main.public_send("image#{sendPiece}")
@photopiece = Base64.strict_encode64(File.read(piece.current_path))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top