开发者

Send Notification and save info to database

I have a form that sends out a notification when a client fills out the form. I know would like to be able to save this information in my database table but am having an issue. It doesn't save a copy to the database at all unless I clear out my model. here's what the controller and model look like.

class ContactController < ApplicationController

  def index
    @contact = Contact.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @contact }
    end
  end

  def create
    @contact = Contact.new(params[:contact])

    if verify_recaptcha(request.remote_ip, params)[:status] == 'false'
      render 'index', :layout => '/layouts/application.html.erb'
  elsif
    respond_to do |format|
      if @contact.save
        format.html { redirect_to("/contact", :notice => 'Your Message was succe开发者_开发技巧ssfully sent.') }
      else
        format.html { render :action => "index" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end
end

end

model

class Contact < ActiveRecord::Base

  include ActiveModel::Validations

  validates_presence_of :email, :phone, :phone_type, :address, :fullName, :content, :userBrowser, :userOS

  attr_accessor :id, :email, :phone, :phone_type, :address, :fullName, :content, :userBrowser, :userOS

  def initialize(attributes = {})
    attributes.each do |key, value|
      self.send("#{key}=", value)
    end
    @attributes = attributes
  end

  def read_attribute_for_validation(key)
    @attributes[key]
  end

  def to_key
  end

  def save
    if self.valid?
      Notifier.contact_notification(self).deliver
      return true
    end
    return false
  end
end

all help is appreciated!


I'm not sure why your overriding so much of the parent methods, all you should need to make the notifier work is

class Contact < ActiveRecord::Base
  validates_presence_of :email, :phone, :phone_type, :address, :fullName, :content, :userBrowser, :userOS

  after_save :send_contact_notification

  def send_contact_notification
    Notifier.contact_notification(self).deliver
  end
end

Also you shouldn't have to include the Validations, they're available already, and if your trying to protect the model fields, you probably want attr_accessible not attr_accessor, since the later is taken care of by ActiveRecord already.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜