2011-06-23 18 views
0

クライアントがフォームに記入すると通知を送信するフォームがあります。私はこの情報を私のデータベーステーブルに保存することができますが、問題があることを知っています。モデルを消去しない限り、データベースにコピーを保存しません。コントローラーとモデルの外観は次のとおりです。すべてのヘルプは高く評価され通知を送信して情報をデータベースに保存

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 

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 successfully sent.') } 
     else 
     format.html { render :action => "index" } 
     format.xml { render :xml => @contact.errors, :status => :unprocessable_entity } 
     end 
    end 
    end 
end 

end 

モデル!

答えて

1

あなたは、あなたが通知の仕事をする必要がある必要があり、すべてが

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 

また、あなたは彼らがしている、検証を含める必要はありませんが、親メソッドのあまりをオーバーライドなぜ私はわからないんだけど既に使用可能であり、モデルフィールドを保護しようとしている場合、attr_accessibleはattr_accessorではなく、後でActiveRecordによって処理されるため、おそらくattr_accessibleが必要になります。

+0

私はもともとデータを電子メールで送信したチュートリアルに従っていただけで、データは保存されておらず、新しいものであり、どのように動作するのか把握しようとしています。 – mediarts

関連する問題