module Resourceful::Default::Responses
Public Class Methods
This method is automatically run when this module is included in Resourceful::Base
. It sets up the default responses for the default actions.
# File lib/resourceful/default/responses.rb, line 53 def self.included(base) base.made_resourceful do response_for(:show, :index, :edit, :new) do |format| format.html format.js end response_for(:show_fails) do |format| not_found = Proc.new { render :text => I18n.t('make_resourceful.show.fails', :default => "No item found"), :status => 404 } format.html ¬_found format.js ¬_found format.xml ¬_found end response_for(:create) do |format| format.html do set_default_flash :notice, I18n.t('make_resourceful.create.success', :default => "Create successful!") set_default_redirect object_path end format.js end response_for(:create_fails) do |format| format.html do set_default_flash :error, I18n.t('make_resourceful.create.fails', :default => "There was a problem!") render :action => :new, :status => 422 end format.js end response_for(:update) do |format| format.html do set_default_flash :notice, I18n.t('make_resourceful.update.success', :default => "Save successful!") set_default_redirect object_path end format.js end response_for(:update_fails) do |format| format.html do set_default_flash :error, I18n.t('make_resourceful.update.fails', :default => "There was a problem saving!") render :action => :edit, :status => 422 end format.js end response_for(:destroy) do |format| format.html do set_default_flash :notice, I18n.t('make_resourceful.destroy.success', :default => "Record deleted!") set_default_redirect objects_path end format.js end response_for(:destroy_fails) do |format| format.html do set_default_flash :error, I18n.t('make_resourceful.destroy.fails', :default => "There was a problem deleting!") set_default_redirect :back, :status => :failure end format.js end end end
Public Instance Methods
Sets the default flash message. This message can be overridden by passing in an HTTP parameter of the form “_flash” via POST or GET.
You can use this to easily have multiple forms post to the same create/edit/destroy actions but display different flash notices - without modifying the controller code at all.
By default, the flash types are notice
when the database operation completes successfully and error
when it fails.
# File lib/resourceful/default/responses.rb, line 19 def set_default_flash(type, message) flash[type] ||= (params[:_flash] && params[:_flash][type]) || message end
Sets the default redirect (the argument passed to redirect_to
). This message can be overridden by passing in an HTTP parameter of the form “_redirect_on” via POST or GET.
You can use this to easily have multiple forms post to the same create/edit/destroy actions but redirect to different URLs
- without modifying the controller code at all.
By default, the redirect statuses are success
when the database operation completes successfully and failure
when it fails. Use the :status
option to specify which status to run the redirect for. For example:
set_default_redirect "/posts", :status => :failure
This will run redirect_to params[:_redirect_on][:failure]
if the parameter exists, or redirect_to "/posts"
otherwise.
# File lib/resourceful/default/responses.rb, line 46 def set_default_redirect(to, options = {}) status = options[:status] || :success redirect_to (params[:_redirect_on] && params[:_redirect_on][status]) || to end