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
Related
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.
I've created a generator that inherits of Rails::Generators::ScaffoldControllerGenerator.
Rspec scaffolding generator is invoked by Rails' generator. I want to override one of its templates.
I've successfully put my new template in this folder: lib/templates/rspec/scaffold/.
However, now when I use Rails's generator my new template is used instead of Rspec' default one.
Is there a way to override a template only during the execution of a specific generator?
I am really new to rails and working to customize a project. I generated a new scaffold, called it New_Scaffold. When the New_Scaffold's index.html.erb is displayed, the wrapper of the application.html.erb (where the yield method is called) is not displayed.
I thought it was automatic in rails, is it not ? How can I display the application.html.erb to wrap the New_Scaffold's index.html ?
The scaffold command generates a bunch of things for a model and controller for you automatically.
If you want to try Rails out try to create a new project like this:
rails new blog
cd blog
rails generate scaffold Post title body:text
rake db:migrate
rails server
This will create a basic project with one model, Post, that will have a generic setup with views and a controller that responds to all the RESTful actions.
the application.html.erb file can be found under views/layouts
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.