How can I use Cloudinary to output url with string conversion?

I will predestinate this by saying that this may be wrong. What I'm trying to do is pass the url w / transform to JS using the data- attribute.

Currently, the following is used to create an image tag:

 = cl_image_tag(image.asset.filename.to_s, transformation: "scroller", :"data-medium" => image.asset.filename.to_s) 

What produces this:

 <img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="s1ufy3nygii85ytoeent.jpg"> 

What I would like to do is show it (using the t_medium named transition that I configured):

 <img src="http://res.cloudinary.com/bucket/image/upload/t_scroller/v1373070863/s1ufy3nygii85ytoeent.jpg" data-medium="http://res.cloudinary.com/bucket/image/upload/t_medium/v1373070863/s1ufy3nygii85ytoeent.jpg"> 

cl_image_tag is cl_image_tag doing the hard work of creating an image tag with a properly configured URL. This is great, however I cannot find any documentation on how to output the configured URL as a string without an image tag (use data-medium as an attribute). I could manually configure the url, but I was wondering if there is a better way?

+4
source share
2 answers

You can use the cloudinary_url helper to create a URL without an image tag. For instance:

 cloudinary_url(image.asset.filename.to_s, transformation: "medium") 

As zeantsoi said, if you use CarrierWave, you can also pass the bootloader itself as a parameter:

 cloudinary_url(image.asset, transformation: "medium") 
+9
source

In response to Tal Lev-Ami's answer:

If you need to call cloudinary_url outside the view (for example, in the serializer model for api), you have 2 options:

  • rely on helper: helper.cloudinary_url
  • or use Cloudinary::Utils.cloudinary_url
+4
source

Source: https://habr.com/ru/post/1490159/


All Articles