Very simple:
rails new myapp
cd myapp
rails generate resource Books title:string
Now look at app/controllers/books_controllers.rb and I find a two line class without any actions. From my reading of the doc, it seems to be expected to generate REST actions.
It seems the rails generate resource does not create any methods or views.
Try running
rails generate resource -h
You will see this
Unlike the scaffold generator, the resource generator does not create
views or add any methods to the generated controller.
What you need is a scaffold generator
rails generate scaffold Book title:string
I believe the correct syntax that you must use is scaffold instead of resource this will create the controller, model and migration. To sum it up the code you must use is rails generate scaffold Book title:string
Pass an extra parameter --skip-template-engine if you do not want the views to be generated.
Related
I want to generate scaffold with a custom namespace but I want it to use an existing model/table.
If I generate a scaffold like this it generate scaffold with a custom namespace but use a new table admin_locations :
rails generate scaffold Admin::Location
Is there a way to do the same thing but using the existing Location model and locations table ?
As suggested by #engineersmnky the best way to do this is :
rails g controller Admin::Locations new create edit index destroy
Then I suggest to create a blank rails project and do :
rails g scaffold Admin::Location
After this you can replace views by the ones generated on the newly created app (/app/views/admin/locations)
Maybe there is a better solution, I would love to know if it's the case !
I am looking to not only customise my erb scaffold templates but also to add new templates.
In ScaffoldGenerator < Erb::Generators::Base I can see there there is a way to provide additional templates in %w(index edit show new _form).
So I have created a custom erb generator and templates directory in my application in lib/generators/erb/scaffold/.
However when I run rails g scaffold Something, my custom generator is not picked up by via scaffold_controller. Does this mean I need to provide a custom scaffold generator to use a custom scaffold_controller generator just so it can then use my erb generator?
I can also see the scaffold argument:
ScaffoldController options:
-e, [--template-engine=NAME] # Template engine to be invoked
# Default: erb
Am I able to provide my erb generator as the template engine to be used?
(Using Rails 4)
lib/templates/erb/scaffold is the correct path for rails scaffold.
Just in case...
model goes in /lib/templates/active_record/model/model.rb
controller goes in /lib/templates/rails/scaffold_controller/controller.rb
I have an API that has many controllers and models.
I installed Rspec and every new resource I create, the corresponding test files are created automatically.
Is there a way to generate these files for all others old resources including REST tests?
For instance, I have a file costumers_controller.rb created before I install Rspec, is there a way to generate the default file costumers_controller_spec.rb?
I found the answer:
rails g rspec:scaffold controller_name
This generate also the request and routing file.
In order to generate the model just use:
rails g rspec:model model_name
And all test files will be ready to use.
Thanks.
Generate a controller with simple spec examples for the specified actions:
rails g rspec:controller Posts index show create
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.
Is there a way to generate a scaffold with the admin folder prefix..For example I want one Admin folder and a few controllers for that admin folder. I want to do a scaffold for each controller in the admin folder and i wanted to know if there was something like
script/generate scaffold admin:something somefield:string
The scaffold generator can take a namespaced argument:
# Rails 3
rails g scaffold Admin::Something somefield:string
# Rails 2
script/generate scaffold Admin::Something somefield:string
Note, that if you'd like to leave model unprefixed, you have to edit some of generated files by hand to implement it. That is why I created rails-admin-scaffold gem and wrote detailed article about custom admin area scaffolding. If that's already not relevant for you, maybe it'll help someone else.