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
Related
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
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
Hey Guys, I'm writing a custom Generator(named shcaffold) in Rails 3.0.3, and I'd like it to generate an active_record model(and migration) based on the first argument passed into it(the model's name).
However, I'm getting this error when I run the command:
$ rails g shcaffold someclass
error active_record [not found]
Here's my generator definition, stored in lib/generators/shcaffold/shcaffold_generator.rb:
class ShcaffoldGenerator < Rails::Generators::NamedBase
include Rails::Generators::ResourceHelpers
source_root File.expand_path('../templates', __FILE__)
# Run Other Generators
hook_for :model, :in => :rails, :required => true
end
I'm defining the orm in my app's application.rb:
config.generators do |g|
g.orm :active_record
g.template_engine :erb
g.test_framework :test_unit, :fixture => false
g.stylesheets false
end
But alas, I'm not having any luck.
No detailled answer, but you should take inspiration there: https://github.com/ryanb/nifty-generators
Another similar question regarding the the error active_record [not found] pain in the nether regions =)
Over here: Rails: hook_for :orm not finding active_record
How to resolve:
Stay within the Rails::Generators namespace
This already solves most of the not-found issues.
configure your generator as a default. (anywhere within the Generators model space)
module Rails
module Generators
class Railtie < ::Rails::Engine
if config.respond_to?(:app_generators)
config.app_generators.orm = :my_own_model
else
config.generators.orm = :my_own_model
end
end
end
end
And you wont have any further hassles from rails g anymore.
nJoy!