Access Asset Path from Rails Controller - ruby-on-rails

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.

Related

Heroku: restrict access to files in public/ by IP?

I've got an app on Heroku that contains uglified JS code. I'd like to include my original sources (.js) in my public folder so that I can refer to them from a source map for debugging. I don't want the source files to be viewable by just anyone, however: I'd like to restrict access to a certain set of IPs.
In other words, in my Rails app on Heroku I'd like to have a file here:
myapp.herokuapp.com/unminified_sources/my_file.js
And I'd like to restrict access to this file to a certain IP (mine).
Is this possible on Heroku? How? Can I use an .htaccess file?
You can put the file behind a unminified_sources_controller my_file action that responds to js requests and restrict it that way. You can restrict in the route or add a before_filter to test for IPs.
You can stream the file. http://guides.rubyonrails.org/action_controller_overview.html#sending-files

ASP.NET MVC How to display Image from folder outside webroot

Individual Folder create for each user when they register on website outside the webroot folder.I am able to upload the image and store Image path in database.However,I am unable to display the Image. I am trying to get the path of Image from database using LINQ and display in VIEW but it does not work. I can view only one(from the list of Image)Image when FILESTREAM the image path and retrun using FILESTREAMRESULT
Can anyone please guide me how to achieve it? All I want to do is create folder for each respective user when they register in external folder. Upload Image and display.
Typically, I would keep files in a secure database or within the application's subfolders. But, here's what I would suggest if you really need to access a different folder on the server.
Create a folder on your server's file system ahead of time.
On the server, assign permissions to that folder so that the identity used by your application can access it.
In your code, write code to access the folder and file path.
Hope that helps!
p.s. here's another Q&A on StackOverflow (with some words of warning) in which the second part of the answer is similar to what I suggested.
ASP.NET - Reading and writing to the file-system, outside the application

How to access uploaded files from views

All the uploaded files are stored under
__namespace__/src/__namespace/Controller/logos/file.jpg
and controller file returns only file name "file.jpg" in the view.
So how can I provide path to logo folder in my view in IMG tag
This is where you made the first "Error"! Never store user-data inside your Modules!
When you have users upload data for your module, store them under /data/module-name/! That way you can easily access the files via src="../data/module-name/filename.jpg"
Alternatively, when you want to provide files with your Module and be able to use them, theres a great Module out there called AssetManager, which in turn uses Assetic.

Redirecting to images path via routes.rb in Ruby

I'm writing a Ruby application and using a 3rd party script that loads images.
The images are located at "/images/modern_images/azure/" for example:
"/images/modern_images/azure/border.jpg"
The script runs in a page located in the "/events/" path and due to a bug, it tries to access the images in:
"/events/modern_images/azure/border.jpg"
The root path is wrong and I can not change the script as it is minified.
How can I do the redirection in routes.rb instead?
Thanks for the help!
modify script& replace 'events' with 'images'. You can make use of :%s/word/word_to_replace inside editor.

How To Dynamically Route to Downloads

Basically, this is what my app does:
It sends an AJAX request
The server creates a file
The server sends back the URL of the
file location
The client-side will attempt to
create a dialog to download the file
at that location (probably using a
frame? I haven't got this far yet).
My question is, how do I dynamically route to the files I create so that they are accessible when you browse to them? If I don't add a route for them, then they will get a 404 if they try and access the directory they're in.
The files are currently stored in a folder in public.
Would the best way to deal with this make the folder somehow not require a route, so that it can be browsed to directly, and then have an index page on it so they can't view the full list of files? If so, please let me know how I can accomplish this. And on a side note, if you have an idea of how I can accomplish JS displaying the download dialog let me know.
It's Rails 3 by the way.
Thanks!
For a full private set of files: choose a place for your files outside your public directory, then configure X-SendFile support in your web server and finally use send_file in your rails application.

Resources