Module: ActsAsMultipartForm::MultipartFormInController::ClassMethods

Defined in:
lib/acts_as_multipart_form/multipart_form_in_controller.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_multipart_form(*args) ⇒ Object

Sets up the multipart form handler with the data needed to create and move through a multipart form The arguments takes several important values including: name: The name of the multipart form. This creates a controller action with that name that can be used in routes and other situations parts: An array of multipart form parts. Each part must have a corresponding part_name and part_name_update method model: The name of the model used to identify the multipart form. Defaults to the singularized name of the controller. form_route: The route for the form as specified in the config/routes file. Defaults to model_form_name downcased. show_route: The route the form redirects to once the last part is filled out. Defaults to the name of the model, downcased.

The args parameter is an array of hashes and multiple multipart forms can be specified with a single acts_as_multipart_form call. To keep the lines from being too long, acts_as_multipart_form can be called multiple times to setup the forms

sample set of multipart form actions def person_info

@person = Person.find(params[:id])

end

def person_info_update

@person = Person.find(params[:id])
@person = Person.new if @person.nil?
@person.multipart_form_controller_action = "person_info_update"

valid = @person.update_attributes(params[:person])
return {:valid => valid}

end

def job_info

@job_position = JobPosition.new
@job_position.person = Person.find(load_multipart_form_data(form_instance_id, :person))
@job_position.multipart_form_controller_action = "job_info_update"

end

def job_info_update

valid = @job_position.update_attributes(params[:job_position])
return {:valid => valid}

end

Parameters:

  • args (Array)

    An array of hashes that determines the data for a multipart form



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/acts_as_multipart_form/multipart_form_in_controller.rb', line 54

def acts_as_multipart_form(*args)

  mattr_accessor :multipart_forms unless self.respond_to?(:multipart_forms)
  self.multipart_forms = {} unless self.multipart_forms.is_a?(Hash)
  
  mattr_accessor :stay_string unless self.respond_to?(:stay_string)

  forms = [] 
  args.each do |arg| 
    # add the update parts
    parts = arg[:parts]
    arg[:parts] = []
    parts.each do |part|
      arg[:parts] << part
      arg[:parts] << (part.to_s + "_update").to_sym
    end
    # sets default model if it is not set
    arg[:model] = self.to_s.gsub("Controller", "").singularize unless arg.has_key?(:model)
    arg[:form_route] = (arg[:name].to_s + "_" + arg[:model].gsub("::", "").underscore) unless arg.has_key?(:form_route)
    arg[:show_route] = (arg[:model].gsub("::", "").underscore) unless arg.has_key?(:show_route)
    # copy args to fields
    self.multipart_forms[arg[:name]] = arg
    forms << arg[:name]

    self.stay_string = arg[:stay_on_button]
  end

  before_filter :multipart_form_handler, :only => forms

  include ActsAsMultipartForm::MultipartFormInController::InstanceMethods
  
end