Skip to content

Instantly share code, notes, and snippets.

@RudolfHattenkofer
Created October 7, 2013 07:52
Show Gist options
  • Save RudolfHattenkofer/6864017 to your computer and use it in GitHub Desktop.
Save RudolfHattenkofer/6864017 to your computer and use it in GitHub Desktop.
Resourceable module for Rails ActiveRecord-Model controllers
module Resourceable
protected
def create_response( saved, resource, format, name, url = false )
if saved
format.html { redirect_to url ? url : resource, notice: name + ' was successfully created.' }
format.json { render action: 'show', status: :created, location: resource }
else
render_error( format, 'new' )
end
end
def update_response( resource, format, params, name, url = false )
if resource.update( params )
format.html { redirect_to url ? url : resource, notice: name + ' was successfully updated.' }
format.json { head :no_content }
else
render_error( format, 'edit' )
end
end
def destroy_response( url, format )
format.html { redirect_to url }
format.json { head :no_content }
end
def render_error( format, action )
format.html { render action: action }
format.json { render json: resource.errors, status: :unprocessable_entity }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment