Created
October 7, 2013 07:52
-
-
Save RudolfHattenkofer/6864017 to your computer and use it in GitHub Desktop.
Resourceable module for Rails ActiveRecord-Model controllers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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