Module: Formize::ActionController
- Defined in:
- lib/formize/action_controller.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
-
#save_and_respond(resource, options = {}, &block) ⇒ Object
Adds method to provides a default response for create/update actions It saves the record/resource and return response with good status and headers.
Class Method Details
.included(base) ⇒ Object
5 6 7 |
# File 'lib/formize/action_controller.rb', line 5 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#save_and_respond(resource, options = {}, &block) ⇒ Object
Adds method to provides a default response for create/update actions It saves the record/resource and return response with good status and headers
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/formize/action_controller.rb', line 11 def save_and_respond(resource, ={}, &block) creation = resource.new_record? resource.attributes = [:attributes] unless [:attributes].nil? respond_to do |format| # if ((block_given? and block.arity == 1) ? yield(resource) : (block_given? and block.arity == 2) ? yield(resource, format) : resource.save) if (block_given? ? yield(resource, format) : resource.save) status = (creation ? :created : :ok) response.headers["X-Return-Code"] = "success" response.headers["X-Saved-Record-Id"] = resource.id.to_s format.html { params[:dialog] ? head(status) : redirect_to([:url] || resource) } format.json { render :json => resource.to_json, :status => status, :location => resource} format.xml { render :xml => resource.to_xml, :status => status, :location => resource } else response.headers["X-Return-Code"] = "invalid" format.html { render :action => (resource.new_record? ? "new" : "edit")} format.json { render :json => resource.errors.to_json, :status => :unprocessable_entity } format.xml { render :xml => resource.errors.to_xml, :status => :unprocessable_entity } end end end |