I'm using Rails 4
And refered these solutions by not works for me
ref
Run scaffold by
rails g scaffold Tool::Mvaas::P2pQuery --no-stylesheets user_id:integer interval:integer
But still generated scaffolds.css.scss for me
conflict app/assets/stylesheets/scaffolds.css.scss
Overwrite /Users/hsu-wei-cheng/workspace/Rails/dqa_dev_server/app/assets/stylesheets/scaffolds.css.scss?
Update
You can disable scaffold stylesheet placing this code on the file config/initializers/assets.rb:
Rails.application.config.generators do |g|
g.scaffold_stylesheet false
end
read this guides chapter "3.3 Configuring Generators".
Place into config/application.rb this code
config.generators do |g|
g.stylesheets false
end
Related
When I use rails g scaffold Model key:string value:string command it creates both controller and views (erb, scss, js). How can I generate only controller which will respond with only JSON format.
Use API flag in Rails 5+
Without having to modify configuration:
rails g scaffold TestLink --api
Pro tip: Use the --pretend flag to see what will be created before running:
rails g scaffold TestLink --api --pretend
If you already have a project setup as a normal rails app, you can make the generators api only by setting it up in config/application.rb.
config.generators do |g|
g.api_only = true
end
To change the Rails controller generated by the scaffold you can add a new file: lib/templates/rails/scaffold_controller/controller.rb. It might be helpful to copy the template from the Rails source code as a starting point. From there you can edit each of the actions to end with render json, for example:
# GET <%= route_url %>
def index
#<%= plural_table_name %> = <%= orm_class.all(class_name) %>
render json: #<%= plural_table_name %>.to_json
end
To prevent asset/view files from being generated, add this to your config/application.rb file:
module AppName
class Application < Rails::Application
# comments and config
# ...
config.generators do |g|
g.stylesheets false
g.javascripts false
g.helper false
g.views false
end
end
end
In rails 4.2.0/ rspec 3.2.2/ rspec-rails 3.2.1. I'm trying to disable specs being generated when I generate new models. I'm using a spec folder structure that is different than the rails convention and would like to not have delete/move generated spec files for every new model. I tried to add generator configuration as mentioned in the rails guides and in What is the syntax to skip creating tests, assets & helpers when running `rails generate controller`?
My config/application.rb contains this:
config.generators do |g|
g.test_framework :rspec
g.model_specs false
g.view_specs false
g.helper_specs false
g.controller_specs false
g.model_spec false
g.helper_specs false
g.request_specs false
g.feature_specs false
end
and I'm still getting:
$rails g model category
invoke active_record
create db/migrate/20150416174523_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Even if I explicitly add tags:
$rails g model category --no-model-specs
invoke active_record
create db/migrate/20150416174908_create_categories.rb
create app/models/category.rb
invoke rspec
create spec/models/category_spec.rb
invoke factory_girl
create spec/factories/categories.rb
Anyone solved this before?
Simply set test_framework to something falsey if you want to disable all generators:
config.generators do |g|
g.test_framework nil
end
You sadly can't disable model specs from the generators alone. They're not optional.
In my Rails 3.2 app's application.rb I have the following lines to disable scaffold generators I don't want:
module MyApp
class Application < Rails::Application
# rest of the config...
config.generators do |g|
g.helper false
g.stylesheets false
g.javascripts false
end
end
end
The app is using the Draper gem and if I run rails generate then decorator is listed as one of the available generators. I assumed that adding g.decorator false to the above list would prevent rails generate scaffold SomeModel from generating the decorator files but they're still created. Can anyone tell me what I'm missing please?
Draper is configured to have decorators built by default for every controller. You can change the default configuration with one additional line in your application.rb file...
module MyApp
class Application < Rails::Application
# rest of the config...
config.generators do |g|
g.helper false
g.stylesheets false
g.javascripts false
g.decorator false
end
end
end
Here's the interesting bit from Draper...
https://github.com/drapergem/draper/blob/master/lib/generators/controller_override.rb
Called from the Railtie...
https://github.com/drapergem/draper/blob/master/lib/draper/railtie.rb
Note that you can still generate decorators explicitly...
$ rails generate decorator foo
I read the help & tried the following command to skip generation of tests, assets & helper files
$ bin/rails generate controller home index --helper false --assets false --controller-specs false --view-specs false
create- app/controllers/home_controller.rb
route get "home/index"
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke rspec
error false [not found]
error false [not found]
As you may notice by output above this works & only controller, routes & views are generated. But as last two lines are interesting:
error false [not found]
error false [not found]
Obviously rails doesn't seem to like --option-name false syntax. so this this error because I used the wrong syntax? If yes, then what is the correct way? Thanks
Try using --no- followed by optionname:
rails generate controller home index --no-helper --no-assets --no-controller-specs --no-view-specs
If you want to change the default behavior every time you run the generator command, you can configure the defaults you would like in the application.rb file - see How can I make sure Rails doesn't generate spec tests for views and helpers?.
To turn off without having to add options:
# application.rb
config.generators.assets = false
config.generators.helper = false
Applications which serve only API will not require javascript, stylesheet, views, helpers. To skip those files in generator/scaffold for Rails 3.x add the below code block in the application.rb
#to skip assets, scaffolds.css, test framework, helpers, view
config.generators do |g|
g.template_engine nil #to skip views
g.test_framework nil #to skip test framework
g.assets false
g.helper false
g.stylesheets false
end
check the link for more details about generators
More concisely:
rails g controller home index --no-assets --no-test-framework
Inside application.rb file write: This will stop generating everything apart from what is written in the command line
config.generators do |g|
g.test_framework nil
g.template_engine nil
g.assets false
g.helper false
g.stylesheets false
g.javascripts false
end
Example:
vidur#vidur-desktop:~/Downloads/tukaweb$ rails g controller uploader/three_d_models
Running via Spring preloader in process 3703
create app/controllers/uploader/three_d_models_controller.rb
invoke assets
invoke js
invoke scss
for one liner solution =>
rails g controller assets_garments --skip-test-framework --skip-assets --skip-helper
If you want to generate only controller, nothing else.
rails g controller [controller_name] [index] --no-helper --no-assets --no-template-engine --no-test-framework
Does anyone know of a rake task or RSpec call that will generate a bunch of empty files relative to the existing controllers, models, helper files and views that already exist within your application?
You can generate an empty scaffold set of rspec tests against an existing controller using something like this:
rails generate rspec:scaffold recipe
You can improve on this by passing the attributes of the model you want to generate against, like this:
rails generate rspec:scaffold recipe title: string slug: string description: text
You'll still need to do some manual editing, but this should get you most of the way there.
The best solution for this is to add hooks in place within environment.rb to create the spec.rb files within the rails application each time a model or controller is created.
Here's the code for that (using RSpec and FactoryGirl):
module RailsApp
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
This should work:
Install the rspec-rails gem by adding it to your development and test groups in your gemfile gem 'rspec-rails'
Run the rspec generator from inside your app rails generate rspec:install
Read over this doc quickly to see how it integrates with your rails app RSpec-rails doc