Rails validation from controller -


there contact page, offers enter name, telephone, email , message, after sends administrator's email. there no reason store message in db.

question. how to:

  1. use rails validations in controller, not using model @ all, or

  2. use validations in model, without db relations

upd:

model:

class contactpagemessage include activemodel::validations include activemodel::conversion extend activemodel::naming  attr_accessor :name, :telephone, :email, :message validates :name, :telephone, :email, :message, presence: true validates :email, email_format: { :message => "Неверный формат e-mail адреса"}  def initialize(attributes = {})     attributes.each |name, value|       send("#{name}=", value)     end end  def persisted?   false end end 

controller:

def sendmessage cpm = contactpagemessage.new() if cpm.valid?     @settings = setting.first     if !@settings         redirect_to contacts_path, :alert => "fail"     end     if contactpagemessage.received(params).deliver         redirect_to contacts_path, :notice => "success"     else         redirect_to contacts_path, :alert => "fail"     end else     redirect_to contacts_path, :alert => "fail" end end end 

you should use model without inheriting activerecord::base class.

class contactpagemessage    include activemodel::validations   include activemodel::conversion   extend activemodel::naming    attr_accessor :whatever    validates :whatever, :presence => true    def initialize(attributes = {})     attributes.each |name, value|       send("#{name}=", value)     end   end    def persisted?     false   end  end 

through able initialize new object , able call validations on object.

i think have different class name same name, in controller code, can see :

if contactpagemessage.received(params).deliver     redirect_to contacts_path, :notice => "success" else 

if mailer class change name contactpagemessagemailer. no loger error.

hope help. thanks


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -