Validation for a non active record input field


#1

I am developing a classifieds type application. It has a Classified
Model, Classified controller and a “show” view.

When viewing an ad, it has an option to reply to the advertiser through
action mailer. This reply is a form that contains 2 text fields ( email
and message) not connected to any table.

How do i validate this email and message?. I was told that
attr_accessor can be used but this doesn’t work quite well as I am
unable use it in Classified Model because when an advertiser edit’s
his/her ad, it throws a validation that “Message” cannot be blank ( this
message field is the one that should be used for replying to the
advertiser).

any idea is much appreciated.


#2

not even a single person implemented non-active record field
validation?..please help


#3

Hi there,

I’ve never done something like that but I can imagine you could do the
validation in your controller. When the action is invoked you can
check the values received in the params hash and act accordingly.

Pepe

On Mar 21, 12:42 pm, Rails L. removed_email_address@domain.invalid


#4

pepe wrote:

Hi there,

I’ve never done something like that but I can imagine you could do the
validation in your controller. When the action is invoked you can
check the values received in the params hash and act accordingly.

Pepe

On Mar 21, 12:42�pm, Rails L. removed_email_address@domain.invalid

Thanks a million. Worked as I wanted (how did I miss this approach?)
:slight_smile:


#5

:slight_smile:

On Mar 23, 2:06 pm, Rails L. removed_email_address@domain.invalid


#6

What you should have is a new model (e.g. AdReply), so that that form
points to AdRepliesController#create, which then attempts to create
the model, and redirects wherever you need to if successful/failed.
All the validations are then on that model and are independent of your
ClassifiedModel.

Cheers,

– D