Change pseudo-absolute assets path in Rails 3 - ruby-on-rails

I tried every way I found, but I don't know how to do it.
Rails always writes /assets/... and would like to have ./assets/ or assets/ because my application is in a subfolder, not in the root folder.
I tried with config.action_controller.asset_host = "." but Rails writes http://./assets/
I tried with config.assets.prefix = "." but Rails writes /./assets/
Suppose that my application lives in: http://domain.com/example/, with the default behaviour of Rails every path points to http://domain.com/ and not to http://domain.com/example/. I don't want to specify an absolute path, because that is not a portable solution.
Check this as an example: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag

You should be able to edit your environment file (config/environments/development.rb) under Rails 3, adding the line:
config.action_controller.relative_url_root = '/example'
This line allows you to run in a subdirectory called "example." Note that you'll want to duplicate this change in production.rb and test.rb as well.

Related

Rails asset pipeline doesn't serve images

My Rails app doesn't serve images at all.
image_url('picture.jpg')
# will result in url(http://localhost:3000/images/picture.jpg)
# but should be url(http://localhost:3000/assets/picture.jpg)
image_tag 'picture.jpg'
asset_url 'picture.jpg'
# will result in the same url / path as image_url()
Neither http://localhost:3000/images/picture.jpg nor http://localhost:3000/assets/picture.jpg exists, while http://localhost:3000/assets/images/picture.jpg does.
Here is a gist of my application.rb and development.rb: https://gist.github.com/maximski/1ccb75f6f89c02932239
I am in development environment and I don't want to precompile files manually. The app is pretty much much newly generated so the configuration is almost completely set on default.
This problem appear if images doesn't exists in app/assets/images directory. Check that app/assets/images/picture.jpg file is exists.

Skip link default behavior rails 4.2

I have a little problem: I have a file stored in public/uploads//paper/foo/bar. This file is stored here by carrierwave (gem for file upload). Now, I want to put a link in my project to this file, but if I do like this: <%=a.name%> I get a routing error like this: No route matches [GET] "/uploads/paper/foo/bar/file.pdf".
How can I skip this default behavior of links in rails?
This probably due to config.serve_static_files is not set to true in config/enviroments/development.rb or whichever environment you are running (defaults to true for development and false for production)

How to override public folder path in Rails 4?

I would like to use a different public folder from a parent directory called client which contains the entire AngularJS app. Essentially I want to tell Rails to load AngularJS app and the only job that Rails has to do is serve JSON.
Is that possible in Ruby on Rails?
As others have mentioned, it may or may not be a great idea to override the existing paths['public'] folder. But you can do the following safely in somewhere like application.rb:
Rails.application.config.middleware.insert_after(
ActionDispatch::Static,
ActionDispatch::Static,
Rails.root.join("client").to_s,
Rails.application.config.static_cache_control
)
The public folder is exposed to the web server through the Rack middleware ActionDispatch::Static. There's nothing else special about it, and the above code simply adds another instance of the middleware that points to the directory client. So in the above case, the browser would be able to access everything in public as well as client.
Just had to do it myself for an Angular app.
Put this in your application.rb:
config.serve_static_files = true
paths['public'] = File.join 'client', 'app'
Or if you still use asset pipeline (config.assets.enabled = true):
paths['public/javascripts'] = File.join 'client', 'app', 'scripts'
paths['public/stylesheets'] = File.join 'client', 'app', 'styles'
Would be interesting to know if there are any consequences with the second bit as my front-end is served completely separately thus I keep asset pipeline switched off and use grunt instead.
You can define another path like
# config/application.rb
paths['my_website'] = 'website'
Then you can use this path in your routes like
# routes.rb
get '/my_website', to: redirect('my_website/index.html')

How can I load multiple YAML files in rails application.rb

I have many yaml files in config/ And I want to load all yaml files.
EX: I have two .yml files name is: application.yml and linkedin.yml. I want to load both files with application.rb.
To achieve this goal I Have written code in application.rb:
ENV.update YAML.load_file('config/application.yml')[Rails.env] rescue {}
ENV.update YAML.load_file('config/linkedin.yml')[Rails.env] rescue {}
But this is not appropriate way, Please suggest me how can I load all yaml files access with ENV variable that.
Assuming that your YAML files are placed in the config folder, in your application.rb you can do this right under the requires (before the module definition)
APP_YAML = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'application.yml'))
LINKED_IN = YAML::load_file(File.join(File.dirname(File.expand_path(__FILE__)), 'linked_in.yml'))
This way, you can then access the contents of the file in a constant that is available everywhere in the app ie. LINKED_IN["secret"]
This is a great way to handle constants that you don't want to check in to source control, but actually I've found that using Figaro is the best way to handle constants. Essentially, Figaro will autogenerate/load an application.yml and all you have to do is put your constants in there.
After this, you can access with ENV["LINKED_IN_SECRET"] - as a plus, this emulates how Heroku would do it with their config:set variable system so you don't have to worry about environment changes :)

Rails, Rake, moving a folder to a new location

I need to move a folder from a plugin to the main app/views. I guess using rake to do this with the following command is the easiest way:
require 'fileutils'
FileUtils.mv('/vendor/plugins/easy_addresses/lib/app/views', '/app/views/')
I'm just not sure where to tell script where to look and where to place the folder.
The file I want to move is in the following location: `vender/plugins/easy_addresses/lib/app/views/easy_addresses
easy_ addresses is the name of the folder in views that I want to move to my_app/app/views/
FileUtils.mv('/source/', '/destination/')
There is a constant which has the rails root, just prepend it to your pathes:
File.join(RAILS_ROOT, "app", "views")
Here RAILS_ROOT holds the location "where to look", and using File.join on the path components takes care of concatenating the components using the right path separator suitable for the used system.
In the result the above method call gives you the complete absolute path to "app/views" in your application.
Edit:
In Rails >= 3 you can use Rails.root.join('app', 'views').

Resources