Rails 3: Application not picking up image files - ruby-on-rails

Bit of a newbie question. I can't seem to get my application to pick up images
that I have in a local folder ( public/stylesheets/images/XYZ/*.png). As a result
my main page is rendered without some essential graphics.
In my HAML file, I have tags defined as follows:
%img{:src => '/images/XYZ/scissor.png'}
This leads to calls like:
Started GET "/images/dookum.in/scissor.png' for 127.0.0.1 ....
and error messages like:
ActionController::RoutingError (No route matches "/images/dookum.in/scissor.png')
I don't know why this is happening. Do I need to define RAILS_ROOT? Or change routes.rb? If yes, then how?
Thanks for your help
Abhinav

You may try to add following setting in your environment files
config.serve_static_assets = true

Either you write
= image_tag('scissor.png')
and this will look for the file /public/images/scissor.png, or you should specify stylesheets/images/XYZ/...
Everything under /public is served in development mode, if you want you rails process to serve everything under /public in production-mode, you have to set the config.serve_static_assets to true.

Related

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 do I prevent my routes.rb from intercepting requests coming to /blog?

My /blog directory is just a bunch of static HTML files. That's good.
When I go to localhost/blog it works fine - it renders the index.html for my middleman generated blog. Great.
But when I click on any of the posts, it gives me a routing error:
No route matches [GET] "/blog/2015/03/11/hello_world"
I am pretty sure the reason this is happening is because of one of these rules in my routes.rb:
get '/:friendly_id', to: 'posts#show'
get '/rbt/:name', to: redirect {|path_params, _| "/#{path_params[:name].gsub(/^\d+\-/, '')}" }
get ':name', to: 'posts#show'
I need all of these routes, but I don't want an HTML request to hit my Rack middleware unnecessarily....or worse yet, do a DB query which this error seems to suggest is happening.
How do I confine all requests to /blog/ to just resolve to my public/blog/ directory?
Edit 1
I realize the above description may not be clear. My Rails App isn't a blog, and so the posts you see referenced above, are not posts to the blog. They are posts of another kind, separately managed by the Rails app with a DB and all. I have since added a real /blog which will just be a collection of HTML articles generated by MiddleMan that will sit in my Rails /public/blog folder. The idea being that the HTML files in my /blog directory, should not hit my Rack middleware at all.
You can force rack to serve certain folder as static and routes-ignoring by adding config.middleware.use Rack::Static, urls: ['/blog'], root: 'public' to config/application.rb but imho it's better to setup a web server to intercept and serve /blog earlier than your app does.
And also in your case /blog/2015/03/11/hello_world seems to be a directory name, if you add index.html to the link it should work as you expect, without changing any configuration.
When your app is run by webserver, its webroot will be the public directory. So, if you have your blog directory inside public directory this should work. localhost/blog
Try running with nginx with following config :
root /root/path/to/your_app/public;
Couldn't you create a static_url controller and configure your routes so that
get '/blog', to: 'static_url#show'
Then in your controller have
def show
render file: request.fullpath
end
Sorry I can't test right now but I'm sure you get the idea. Also, I guess you would need to detect non-existent pages.

Rails 3 : Heroku app using myapp.herokuapp.com as base for URL building

I just deployed one of my apps to heroku. This app uses :
A default "myapp.herokuapp.com" address,
And I got a domain configured so that the app can be reached through "www.myapp.com".
And I noticed today the following issue : my application links are based on "http://myapp.herokuapp.com" domain (hence I get "http://myapp.herokuapp.com/page" URLs) even when I access the app using my domain name (I would then expect to get "www.myapp.com/page" URLs).
I tried to edit my production.rb and set the default_url_options :
# Base domain for url generation
config.action_controller.default_url_options = { :host => "www.myapp.com" }
But it doesn't change a thing. Also tried to change this in application.rb, just in case, but nothing happens either.
Any clue ?
Thanks a lot for your help guys !
Edit : This used to work as expected before today when I did the database migration to the new Heroku postgres thing. Don't know if this can have any impact.
If you're using _path methods for your urls, this is generating a relative path which is always based on the url you visit. If you're using controller/fragment caching, you should probably use _url instead in your views. You might also want to consider setting config.action_controller.perform_caching to false in your production.rb if all your pages have some controller logic.
See this page for more info on how caching works in Rails.
I had a similar problem. It was caused by the following line of code which was pointing to heroku.com and getting redirected to herokuapp.com
config.action_mailer.default_url_options = { :host => 'my-staging-domain.heroku.com' }
I mention it because it's the action_mailer.default_url_options yet clearly it affects the default url options outside of the scope of the mailer if you haven't explicitly set up the action_controller.default_url_options

Using url_helpers in a coffeescript file with a base url in production

I am trying to use Rails url_helpers inside of coffescript files. I append the .erb extension to the filename (profile.js.coffee.erb) and use ERB to access the helpers like so:
jQuery ->
window.Intl.Models.Profile = Backbone.Model.extend
url: '<%= Intl::Application.routes.url_helpers.profile_path %>'
This works great in development and it will return /profile. In production, I am using a URL root of /intl so I tell the asset pre-compiler to consider this in deploy.rb
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT='/intl'"
However, this doesn't seem to affect the url_helpers because it will still return /profile instead of /intl/profile
I tried setting ENV["RAILS_RELATIVE_URL_ROOT"] in production.rb and development.rb respectively and that didn't work either.
Any suggestions would be appreciated. Thank you.
You may need to set this at the Rack level. If you are using Passenger, you can set the RackBaseURI directive.
Same problem here.
As a partial solution I use
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT=/csc"
in deploy.rb for Capistrano deploymment. This works fine for all other aspects of the asset precompilation except this single aspect (.coffee.erb) wich seems completely unaffected by that setting.

Rails 3.1.1 - Asset Pipeline - asset.prefix not working with resource :assets

I am having an issue with the asset pipeline, I already have a resource / controller called assets. So i have changed assets.prefix option to "/externals".
config.assets.prefix = '/externals'
This simple dose not work unless i remove:
resources: assets
Then all works as expected.
I am not sure how to write a test to prove this but i have created a app to showcase it.
https://github.com/nodrog/asset-pipeline-issue
If you run the app, and visit '/products' all will work, then go to the routes file and change the variable create_bug to true.
We have looked into https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/helpers/rails_helper.rb, and then added a debugger to the asset_path method.
This method is not called, if you but a debugger in the javascript_include_tag method. And run method(:asset_path).source_location, it tells you it is calling the method from default routes not from the sprockets helper.
Any help would be greatly appreciated...
UPDATE:
I reported this to the rails people, and they fixed it. The fix is now in the master branch.
https://github.com/rails/rails/issues/3643#issuecomment-2775938

Resources