views:

36

answers:

1

Let's say I have a form_tag in a view gathering a view user input . With this input that I get through the params hash, I want to verify some of htis information such as email phone name.

Can I just verify in the controller or is this a bad thing? I don't plan to save the thing I get to a db or anything, it's just getting put into an email and sent off, but I want to verify these things before it's sent.

Thank you.

EDIT:

Found this http://www.viddler.com/explore/rails3/videos/6/

class Test
 include ActiveRecord::Validations
 validates_presence_of :name, :email
attr_accessor :name, :email
end
+1  A: 

You can use the model for whatever you need that is related to the object and don't need to save it. Keeping stuff like this in the model is desirable in order to keep controller tidy. Say you have a user model:

#controller
@user.new params[:user]
@user.check_valid
@user.save # is optional

#user.rb

def check_valid
  !email.blank? and !phone.blank?  
end
mark
Why not just @user.valid? in controller? It looks like check_valid method is just a duplication of native ActiveRecord method..
fantactuka
When I started writing the answer what I had in mind was more flexibility than validating the object. As the user stated, he wants to check 'some of this information'. I'll edit the answer to clarify this.
mark
So I think I get what your saying is, build the model, and use the validates_ commands inside of the model, but just don't save it when the times comes. Thanks for the advice. Sounds like a plan :) Thanks again.
RoR