I'm building an application in Rails where sets of images can be created, i.e. the user uploads a file of image names and specifies a path where those images can be found on the web.
So, for example, the image file contains:
image1.jpg
image2.jpg
and the path is specified as http://www.user1-server.com/.
Another user could load his own file of image names, but specify another server: http://www.user2-server.com/ or even http://my-fancy-server.com.
Is there any way to use the AssetTagHelper functionality of Rails to help me generate the image tags?
So, if I'm in the context of user 1, e.g. /users/1/images/1, and use:
image_tag("1.jpg")
it should deliver http://www.user1-server.com/images/1.jpg, but for /users/2/images/1 it should return http://www.user2-server.com/images/1.jpg or http://my-fancy-server.com/images/1.jpg.
I don't think you can change asset host in Rails on the fly like that. Rails is smart enough to not set or override the host passed into an image tag, though. Maybe just write a helper method or something to pass the correct host in?
Related
I need to access a local JSON file. Since Grails 2.4 implements the AssetPipeline plugin by default, I saved my local JSON file at:
/grails-app/assets/javascript/vendor/me/json/local.json
Now what I need is to generate a URL to this JSON file, to be used as a function parameter on my JavaScript's $.getJSON() . I've tried using:
var URL.local = ""${ raw(asset.assetPath(src: "local.json")) }";
but it generates an invalid link:
console.log(URL.local);
// prints /project/assets/local.json
// instead of /project/assets/vendor/me/json/local.json
I also encountered the same scenario with images that are handled by AssetPipeline1.9.9— that are supposed to be inserted dynamically on the page. How can I generate the URL pointing this resource? I know, I can always provide a static String for the URL, but it seems there would be a more proper solution.
EDIT
I was asked if I could move the local JSON file directly under the assets/javascript root directory instead of placing it under a subdirectory to for an easier solution. I prefer not to, for organization purposes.
Have you tried asset.assetPath(src: "/me/json/local.json")
The assets plugin looks in all of the immediate children of assets/. Your local.json file would need to be placed in /project/assets/foo/ for your current code to pick it up.
Check out the relevant documentation here which contains an example.
The first level deep within the assets folder is simply used for organization purposes and can contain folders of any name you wish. File types also don't need to be in any specific folder. These folders are omitted from the URL mappings and relative path calculations.
I'm sharing a configuration yml file client side, that I need to also load on the server side, I've placed it inside app/assets/javascripts/configuration.yml
I can use #{asset_path 'configuration.yml'} inside a view to get the path, but I can't inside a controller. I could access directly using "#{Rails.root}/app/assets/javascripts/configuration.yml" but when deploying the filename gets the digest string appended.
How can I get the same path from a controller?
ActionController::Base.helpers.asset_path("configuration.yml")
Might also be good to put configuration.yml in a different folder to separate javascript from non-javascript files.
I'm using carrier wave to upload images in rails3,
i want to just check the condition is image available in upload folder or not.
Because in my case suppose image name available in data base but by mistake image deleted from upload folder in that scenario when click on image link getting error .
I think, this can be done using exist?(file_name) method of Ruby File Class. I assume you can get the full path of the file from the database, and pass it to the class method as follows:
File.exist?('full/path/of/the/file')
This will return true if the file exists otherwise false. for reference you can read about File operations here
Carrierwave has a built-in method to do just that. It works whether your storage method is :file or :fog. Looks like this:
picture.data.file.exists?
Does Rails have an equivalent of the Server.MapPath method from ASP.NET? I've tried looking for one, but couldn't find anything.
Edit: I need this to generate a PDF with some images stored on the server. I know the relative path (URL) of the images, but I need an absolute path on disk to load them. I use FPDF for this and even though it says it accepts an URL, it doesn't seem to be the case (or I couldn't make it work).
It checked that it works with a hardcoded physical disk path and now I need to make it flexible.
Ruby has one: File.expand_path.
You can also use Rails.root to get the current path to rails project, then compose the path from there.
File.join(Rails.root, "public", "404.html")
File.expand_path("public/404.html", Rails.root)
There are some image assets on my site that will always remain static, and therefore do not need the timestamp that gets appended to the images. Is there a way to not append the timestamps for certain assets?
Why not just use the regular HTML <img> element for those images? It'll be marginally faster than going through the Rails' helper too.
From the Rails docs "You can enable or disable the asset tag timestamps cache. With the cache enabled, the asset tag helper methods will make fewer expense file system calls. However this prevents you from modifying any asset files while the server is running."
ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = true
Solution 1 would work, except I still need it to use the asset host, and I don't want to hardcode it. Solution 2 does not work since that would affect all asset paths. I think what I should be doing is to combine using the img tag, but use rails to compute the asset host for me.
So in the end it would look something like this
<img src=\"#{#template.image_path("image.jpg}}\"/>
thanks for the idea!