Skip link default behavior rails 4.2 - ruby-on-rails

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)

Related

Ruby on Rails: configure web_console in initializer

I am using the web_console gem and I would like to add some IPs to the whitelist. For reasons that would probably go to far to explain, can't simply add something to the config/application.rb or config/environments/development.rb. However I can create an initializer config/initializers/.
I simple tried this in config/initializers/99-webconsole.rb, but while the file is loaded (--> debug message is shown), the web console does not seem to pick up my settings.
Rails.application.configure do
config.web_console.whitelisted_ips = '10.10.0.0/16'
p "Debug: this is loaded."
end
I assume it's related to some kind of race condition? Providing the same line in config/environments/development.rb works, but as said, I sadly can not change that file.
Based on this code https://github.com/rails/web-console/blob/e3dcf4c588af526eafcf1ce9413e62d846599538/lib/web_console/railtie.rb#L59
maybe there is a code in your initializer that configuring config.web_console.permissions, so your whitelisted_ips config is ignored
whitelisted_ips is also deprecated
and have you checked that you are using v4.2.0, the permissions was buggy and fixed by this commit https://github.com/rails/web-console/commit/6336c89385b58e88b2661ea3dc42fe28651d6296

Zeitwerk error on devise mailer in production environment

I have Rails 6, my preview class located in
mailer/previews/devise_mailer_preview.rb:
class DeviseMailerPreview < ActionMailer::Preview
...
end
And when I run application locally, everything is going fine, I can see my email previews on http://localhost:3000/rails/mailers/devise_mailer/confirmation_instructions address. But now Im trying to deploy application on server, and found that when I run bundle exec rails c production, I got the error:
/home/deploy/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/zeitwerk-2.3.0/lib/zeitwerk/loader/callbacks.rb:17:in
`on_file_autoloaded': expected file
/home/deploy/project/releases/20200627024908/app/mailer/previews/devise_mailer_preview.rb
to define constant Previews::DeviseMailerPreview, but didn't
(Zeitwerk::NameError)
After that I've checked locally RAILS_ENV=production rails c, and got same.
If I will rename DeviseMailerPreview class to Previews::DeviseMailerPreview, it will be broken and I cannot see emails on development, because Rails 6, accordingly to docs, expect exactly that name.
More of that, I've found in this article, that zeitwerk can be configured with autoload_paths param to avoid ruby's NameError. I found that I have it my config/application.rb:
config.load_defaults 6.0
Anyway I tried to add same row in my config/environments/production.rb file, but it didn't help.
What am I doing wrong and how can I fix it? Thanks in advance!
Add preview_path to the autoload_paths and zeitwerk will expect DeviseMailerPreview constant to be defined.
# development.rb
config.action_mailer.preview_path = Rails.root.join("app/mailers/previews")
# application.rb
config.autoload_paths << Rails.root.join("app/mailers/previews")
Your Mailer preview file is located in mailer/previews/devise_mailer_preview.rb, so I'm assuming it's full path is app/mailer/previews/devise_mailer_preview.rb
The docs says
In the above example, the preview class for UserMailer should be named UserMailerPreview and located in test/mailers/previews/user_mailer_preview.rb
So put your devise_mailer_preview.rb file to test/mailers/previews/devise_mailer_preview.rb
or in your config/application.rb add this line and restart:
config.action_mailer.preview_path = "#{Rails.root}/app/mailers/previews"
Actually the answer was in the docs itself.
In Rails 6, previews are added to the autoload paths only if options.show_previews is true, which is not by default in the production environment. See the source code here.
The reason for this is that previews are supposed to be an aid for development, and they are generally not something you want to be able to look at in production.
However, you can set that flag to true in production if you want.
There's another derivative: By storing the previews under app/mailers, Rails is going to eager load them because app/mailers is in the autoload paths. If app/mailers/previews is not in the autoload paths, eager loading will fail due to the namespace mismatch. Either you have them enabled in all environments, or else is better to have them in a separate location, like the default.

Change pseudo-absolute assets path in Rails 3

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.

ActionMailer preview_path is not the RSpec default

I have an app that's throwing the following error every time the environment is loaded.
Action Mailer preview_path is not the RSpec default. Preview path is set to: spec
I'm not even sure what the preview_path here is or where it might be configured. Is there a way to adjust this or disable it?
Thanks!
You probably have the following in your config/environment/development.rb file:
config.action_mailer.preview_path = 'spec/'
As for what the preview path is, it's to preview emails in your browser.
Not sure why it would be erroring. Perhaps try commenting out the config line above if you don't need email previewing?

Rails 3: Application not picking up image files

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.

Resources