| Module | Resourceful::Default::Responses |
| In: |
lib/resourceful/default/responses.rb
|
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 64
64: def self.included(base)
65: base.made_resourceful do
66: response_for(:show, :index, :edit, :new) do |format|
67: format.html
68: format.js
69: end
70:
71: response_for(:show_fails) do |format|
72: not_found = Proc.new { render :text => "No item found", :status => 404 }
73: format.html ¬_found
74: format.js ¬_found
75: format.xml ¬_found
76: end
77:
78: response_for(:create) do |format|
79: format.html do
80: set_default_flash(:notice, "Create successful!")
81: set_default_redirect object_path
82: end
83: format.js
84: end
85:
86: response_for(:create_fails) do |format|
87: format.html do
88: set_default_flash :error, "There was a problem!"
89: render :action => :new, :status => 422
90: end
91: format.js
92: end
93:
94: response_for(:update) do |format|
95: format.html do
96: set_default_flash :notice, "Save successful!"
97: set_default_redirect object_path
98: end
99: format.js
100: end
101:
102: response_for(:update_fails) do |format|
103: format.html do
104: set_default_flash :error, "There was a problem saving!"
105: render :action => :edit, :status => 422
106: end
107: format.js
108: end
109:
110: response_for(:destroy) do |format|
111: format.html do
112: set_default_flash :notice, "Record deleted!"
113: set_default_redirect objects_path
114: end
115: format.js
116: end
117:
118: response_for(:destroy_fails) do |format|
119: format.html do
120: set_default_flash :error, "There was a problem deleting!"
121: set_default_redirect :back, :status => :failure
122: end
123: format.js
124: end
125: end
126: end
Sets the default flash message. This message can be overridden by passing in an HTTP parameter of the form "_flash[type]" 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
19: def set_default_flash(type, message)
20: flash[type] ||= (params[:_flash] && params[:_flash][type]) || message
21: 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[status]" 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
46: def set_default_redirect(to, options = {})
47: status = options[:status] || options[:on] || :success
48:
49: if options[:on]
50: STDERR.puts "DEPRECATION WARNING:\nThe make_resourceful #set_default_redirect :on option\nis deprecated and will be removed in 0.3.0.\nUse the :status option instead.\n".gsub("\n", ' ')
51: end
52:
53: redirect_to (params[:_redirect_on] && params[:_redirect_on][status]) || to
54: end