How to disable the generator of assets on rails 3.2 - ruby-on-rails

Every time I create a controller, rails generate a controler_name.js and a controller_name.css file on app/assets folder. I already disable the config.assets.enabled param on application.rb but this not solve my problem.
How can I disable the generator for those files when creating controller ?
Thanks

You can pass --skip-assets to your command to prevent these files from being created:
rails g controller foo --skip-assets
If you want something more permanent, you can turn it off altogether. Add this to config/application.rb (from How do I turn off automatic stylesheet/javascript generation on Rails 3.1?)
config.generators.stylesheets = false
config.generators.javascripts = false

Related

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.

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)

Don't create view folder on rails generate controller

Is there a way with the usual generators config to turn OFF the creation of the view folders and action templates when you run a rails generate controller?
I can't find an option anywhere and the code here doesn't show me any pointers.
We are likely going to be building our own controller / resource generators at some point anyway, for our API, but I was curious if there was a way to turn off this annoyance in the meantime.
It's not a well documented feature, but try to add --skip-template-engine (alias --no-template-engine) option to the command.
rails generate controller foo bar --skip-template-engine
demo on a dummy app:
rails g controller my_controller index show --no-template-engine
create app/controllers/my_controller_controller.rb
route get "my_controller/show"
route get "my_controller/index"
invoke test_unit
create test/functional/my_controller_controller_test.rb
invoke helper
create app/helpers/my_controller_helper.rb
invoke test_unit
create test/unit/helpers/my_controller_helper_test.rb
invoke assets
invoke coffee
create app/assets/javascripts/my_controller.js.coffee
invoke scss
create app/assets/stylesheets/my_controller.css.scss
To skip views from being generated with your controller, disable the template engine.
Once:
rails g controller ControllerName action1 action2 --skip-template-engine
Note that every --skip option also has an aliased --no option.
Default:
# config/application.rb
config.generators do |g|
g.template_engine false
end
# OR
config.generators.template_engine = false
If you have an API-only application (no front end), you may also want to skip assets and helpers from being generated with your controllers.
Once:
rails g controller api/users --no-helper --no-assets --no-template-engine
Default:
# config/application.rb
config.generators do |g|
g.assets false
g.helper false
g.template_engine false
end
# OR
config.generators.assets = false
config.generators.helper = false
config.generators.template_engine = false
Disabling assets skips stylesheets and javascripts from being generated. If you only want to skip one, use --no-stylesheets or --no-javascripts, or in config/application.rb use:
config.generators.stylesheets = false
config.generators.javascripts = false
If your default configuration skips something from being generated (e.g. assets and helpers) but you need them in one case, you can generate them like so:
rails g controller foo --helper --assets --skip
where --skip skips generating files that already exist.
Just thought I'd try underscoring the --skip-template-engine flag to see if it worked in a generator and it worked a charm! No view templates generated from a bin/rails g controller command in a Rails 4.2 application.
Try:
config.generators do |g|
g.template_engine false
end
A little late I know but these things stick around in Google! ;)
If you're creating an API with no front end, you can go ahead and use rails new --api. However, I don't recommend this option if you do plan to create a front end (for example a single page app) because it turns a lot of things off, including the asset pipeline.

Rails scaffold without the css file?

Is there a way to generate a scaffold in rails 3.0 so that scaffold.css does NOT get created? Something at the command line I can enter to skip that step?
Thanks
There is a --no-stylesheets flag you can use:
rails g scaffold MyModel --no-stylesheets
You can also disable it by default -- in config/application.rb:
config.generators do |g|
g.stylesheets false
end
Rails itself only uses it for scaffold.css AFAIK, but unfortunately the same hook could be used by other generators, so you might need to remember to pass --stylesheets for a third-party gem that generates assets, for instance. It'd be really nice if Rails had an explicit option for scaffold.css :-/
You can find other generator options in the Rails Guides, by the way. Helpers are nice to turn off by default and generate them when you actually want them.
Since Rails 5.0, there is a configuration in config/application.rb which specifically disables generating the app/assets/stylesheets/scaffolds.css, but still generates the stylesheets for your new resource:
config.generators do |g|
g.scaffold_stylesheet false
end
You can also pass it in as the --no-scaffold-stylesheet command line option:
rails generate scaffold post title body:text --no-scaffold-stylesheet

Override default scaffold generator in rails 3

I've created a generator for a controller in rails 3.
Now I want to use this generator as the default generator when using the scaffolding generator.
Is that possible?
The correct position for your customized controller file is lib/templates/rails/scaffold_controller/controller.rb
If you simply want to use your own controller template, you can just put it in lib/templates/rails/scaffold_controller/controller.rb
If you want to replace the scaffold_controller_generator code itself, for example, so that the controller scaffold generates additional class files. you can create lib/generators/rails/my_scaffold_controller/my_scaffold_controller_generator.rb with templates under lib/generators/rails/my_scaffold_controller/templates.
Remember to point rails at your new scaffold_controller in config/application.rb:
config.generators do |g|
g.scaffold_controller = "my_scaffold_controller"
end
For my_scaffold_controller_generator.rb you could copy from the railties gem under railties-3.x.x/lib/rails/generators/rails/scaffold_controller if you want to modify default behaviour, or inherit from it if you just want to add functionality:
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Rails
module Generators
class MyScaffoldControllerGenerator < ScaffoldControllerGenerator
source_root File.expand_path("../templates", __FILE__)
def new_funtionality
end
end
end
end
You can override the templates that Rails uses for its generators. In this instance, just place the file at lib/templates/scaffold_controller/controller.rb and modify it how you wish. The next time you run rails g scaffold [modelName] it will pick up this new controller template and use it.
This is covered in Section 6 of the Creating and Customizing Rails Generators official guide.
This seems to have changed slightly with Rails 4. You can see which template the generator will look for in the invoke line when the scaffold is generated, and your template folder name should match this:
rails generate scaffold blub
...
invoke responders_controller
If you're using rails g scaffold_controller blubs the location of the template should be:
lib/templates/rails/scaffold_controller/controller.rb
If you're using rails g scaffold blub the location of the template should be:
lib/templates/rails/responders_controller/controller.rb
If anyone is wondering why this isn't working in a default Rails 4 install, it's because jbuilder is inserting itself into the template path before the override location. I don't need jbuilder so I removed it, but I also reported an issue in Github. Hopefully it'll be fixed soon.

Resources