Отправить уведомление и сохранить информацию в базу данных - PullRequest
0 голосов
/ 23 июня 2011

У меня есть форма, которая отправляет уведомление, когда клиент заполняет форму.Я знаю, хотел бы иметь возможность сохранить эту информацию в моей таблице базы данных, но у меня возникла проблема.Он не сохраняет копию в базе данных вообще, если я не очищаю свою модель.вот как выглядят контроллер и модель.

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

модель

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

вся помощь приветствуется!

1 Ответ

1 голос
/ 23 июня 2011

Я не уверен, почему вы переопределяете так много родительских методов, все, что вам нужно для работы уведомителя, это

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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...