gem install crud_generator.
Note: This gem is designed for Rails so only Rails setup instructions are provided, though it could work with any MVC framework.
Once you have created your rails app add the following to your Gemfile
gem 'crud_generator', '~>0.0'
Once you have run
bundle install you can use this gem by adding the following to your ApplicationController
This gem allows you to create all and only the standard CRUD actions you want in any controller. If you want to generate all of the standard CRUD actions, you can simply type in the following:
However, if you only want some of the standard CRUD actions, such as
create, you can specify which actions you want in one of the following ways:
First, you can simply type in the CRUD actions you want.
generate_crud_actions :index, :new, :create
Second, you can specify that you only want
generate_crud_actions :only => [:index, :new, :create]
Third, you can specify that you want everything except
generate_crud_actions :except => [:show, :edit, :update, :delete]
If you choose to specify which CRUD actions you want, none of the other methods will be created and you can simply create them yourself however you want.
All of the above explains how to use this gem, but does not show what the gem actually does. Suppose that you are creating a basic posts resource for you application and that you want to have
show pages, in addition to giving your users the ability to
destroy posts (using the related
edit views). You can simply type in
generate_crud_actions into your PostsController and the following controller actions will be created for you:
def index @posts = Post.all end
def show @post = Post.find(params[:id]) end
def new @post = Post.new end
def create @post = Post.new(params[:post]) if @post.save redirect_to @post else flash.now[:errors] = @user.errors.full_messages render :new end end
def edit @post = Post.find(params[:id]) end
def update @post = Post.find(params[:id]) if @post.update_attributes(params[:post]) redirect_to @post else flash.now[:errors] = @post.errors.full_messages render :new end end
def destroy @post = Post.find(params[:id]) @post.destroy redirect_to posts_url end
Creating every CRUD action would work if you want all of the code above. However, if you would prefer to make the
create action more specific, like giving it the ability to make the author of the post the current user, then you should probably specify that you want every CRUD action above except
create and make your own
create action as needed (if you need an example of how to do this, look at the previous section).
#Note on Incorrect Usage
The code is generated based on your controller name and the assumption that there is an associated model and set of routes. This means that you should never create any of these actions if there is no associated model or you do not have the routes that the generated action.