Skip to content

Instantly share code, notes, and snippets.

@col
Last active December 10, 2015 04:18
Show Gist options
  • Save col/4379729 to your computer and use it in GitHub Desktop.
Save col/4379729 to your computer and use it in GitHub Desktop.
Improved scaffold controller generator template. The comments and the json format has been removed and a before filter has been added to fetch the resource. The controller has now also been updated to use the strong parameters gem. This can be added to an existing application at: lib/templates/rails/scaffold_controller/controller.rb
<% if namespaced? -%>
require_dependency "<%= namespaced_file_path %>/application_controller"
<% end -%>
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
include ActiveModel::ForbiddenAttributesProtection
before_filter :find_user, :only => [:show, :edit, :update, :destroy]
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
end
def show
end
def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
end
def edit
end
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
if @<%= orm_instance.save %>
redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully created.'" %>
else
render :new
end
end
def update
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully updated.'" %>
else
render :edit
end
end
def destroy
@<%= orm_instance.destroy %>
redirect_to <%= index_helper %>_url
end
private
def find_<%= singular_table_name %>
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
end
end
<% end -%>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment