A collection of useful Rails generator scripts for scaffolding, layout files, authentication, and more.
An extended verison of the collection of useful Rails generator scripts created by ryanb github.com/ryanb/nifty-generators adding improved controller tests when using Shoulda (github.com/thoughtbot/shoulda), support for using factory_girl (github.com/thoughtbot/factory_girl) to provide the test fixtures, and support for the generation of formtastic (github.com/justinfrench/formtastic/) powered forms in the views.
Available only as a plugin.
To use in a single rails app:
Once you install the gem, the generators will be available to all Rails applications on your system. If you run script/generate without any additional arguments you should see the available generators listed.
script/plugin install git://github.com/iceburging/nifty-generators.git
To make available to all rails apps:
~/.rails/generators install git://github.com/iceburging/nifty-generators.git
To run the generator, go to your rails project directory and call it using the script/generate or script/destroy command.
script/generate nifty_scaffold Recipe name:string index new
Simply use the nifty_scaffold generator with the --shoulda
option as before. The controller tests will now test assignment, response, template rendering and the flash status.
script/generate nifty_scaffold --shoulda Recipe name:string
Use the the nifty_scaffold generator with the --factory_girl
option to create a factory.rb file in test/ (if using Test::Unit or Shoulda) or spec/ (if using RSpec) and use factory_girl to provide the test fixtures.
script/generate nifty_scaffold --factory_girl Recipe name:string
If the factory.rb file already exists then the new Factory will be appended to the end of the file.
Use the the nifty_scaffold generator with the --formtastic
option to generate formtastic code for the forms in the new and edit views.
script/generate nifty_scaffold --formtastic Recipe name:string
When using the formtastic option you will need to make sure that you have installed the formtastic gem, generated the formtastic stylesheets, and added them to your application.rb layout file before running the application.
The --factory_girl
and --formtastic
options should work alongside the other options. Notably they can be used with both the --haml
and --RSpec
options.
-
nifty_layout: generates generic layout, stylesheet, and helper files.
-
nifty_scaffold: generates a controller and optional model/migration.
-
nifty_config: generates a config YAML file and loader.
-
nifty_authentication: generates user model with sign up and log in.
To view the README for each generator, run it with the help
option.
script/generate nifty_layout --help
To use Nifty Generators with Rails 3 you will need to include it in your Gemfile.
gem "nifty-generators"
The generators use a colon as the separator instead of an underscore.
rails g nifty:layout
The Rails 3 support is still in early development, please report any issues you find.
<<<<<<< HEAD What is the difference between nifty_scaffold and built-in scaffold?
One of the primary differences is that nifty_scaffold allows you to choose which controller actions to generate.
script/generate nifty_scaffold post name:string index new edit
There are a few changes to the generated code as well, such as no XML format by default.
It also offers support for HAML, Shoulda, and RSpec.
I get “undefined method ‘title’” error.
Try running nifty_layout, that will generate this helper method. Or you can just change the templates to whatever approach you prefer for setting the title.
I can’t set new attributes in my model.
Add the attribute to the attr_accessible line in the model.
I get “undefined method ‘root_url’” error.
Some generators default redirecting to the root_url. Set this in your routes.rb file like this (substituting your controller name).
map.root :controller => 'foo'
I get a missing database error.
Run rake db:migrate
.
I get a routing error when I try to submit a form.
Try restarting your development server. Sometimes it doesn’t detect the change in the routing.
The tests/specs don’t work.
Make sure you have mocha installed and require it in your spec/test helper.
gem install mocha # in spec_helper.rb config.mock_with :mocha # in test_helper.rb require 'mocha'
Also, make sure you’re using Rails 2.1 or greater.
If you are having a problem with Nifty Generators, first look at the FAQs above. If you still cannot resolve it, please submit an issue here.
github.com/ryanb/nifty-generators/issues If you wish the generators behaved differently, please consider forking the project and modifying to your heart’s content.