Class: Noodall::FormResponsesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/noodall/form_responses_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

POST /responses POST /responses.xml



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/noodall/form_responses_controller.rb', line 6

def create

  # long way of adding a new response to a form (create then append to form model)
  # as redirecting back to the form will cause errors with half built appended objects
  @form = Noodall::Form.find(params[:form_id])

  @form_response = @form.responses.build(params[:form_response])

  #If the form response hasn't been constructed properly (i.e. a random spam POST)
  #raise MongoMapper::DocumentNotFound, "Form response does not match form" unless @form_response.correct_fields?

  @form_response.ip = request.remote_ip
  @form_response.referrer = request.referer if @form_response.referrer.blank?
  @form_response.created_at = Time.zone.now

  respond_to do |format|
    if @form_response.valid? and @form_response.save
      if @form_response.is_spam?
        logger.info "Form response was deemed to be spam: #{@form_response.inspect}"
      else
        begin
          # mail the response to the form recipient
          FormMailer.form_response(@form, @form_response).deliver unless @form.email.blank?
          FormMailer.form_response_thankyou(@form, @form_response).deliver
        rescue Net::SMTPSyntaxError
        end
      end

      format.html
      format.xml  { render :xml => @form, :status => :created, :location => @form }
    else
      format.html { render 'new' }
      format.xml  { render :xml => @form_response.errors, :status => :unprocessable_entity }
    end
  end
end