Validation in controllers

Hi All, I am working on my first RoR application and running into an
issue. I’ve been working through the authentication example in the AWDR
book and have a question about the login form. Basically, there is a
login page that has an email address/password field on it. There is a
login method that basically looks for the user by email address, if they
exist, it attempts to authenticate them. My question is this: since I am
not trying to save a new model, is there a way to take advantage of
active record’s built-in validation? I’d like to be able to validate
that the email/password are populated and that they are valid. I’d also
like to be able to display the error message in the same sort of errors
collection accessible by the model. I noticed I’m not able to access the
‘errors’ collection from the controller. Any help would be appreciated.

Thanks in advance,

J

The ActiveRecord validation is applied when you save a new instance of a
model. For a login feature, you aren’t saving a new model (as you
noted) and therefore it doesn’t apply here.

If you have some particular reason to validate the login credentials
before pulling the user info from the database (why would you want to do
that, though?), you could do a User.new(params).valid?, but that would
give you errors for all of any empty fields you are validating in the
model.

Dave S. wrote:

The ActiveRecord validation is applied when you save a new instance of a
model. For a login feature, you aren’t saving a new model (as you
noted) and therefore it doesn’t apply here.

If you have some particular reason to validate the login credentials
before pulling the user info from the database (why would you want to do
that, though?), you could do a User.new(params).valid?, but that would
give you errors for all of any empty fields you are validating in the
model.

The purpose is determine whether or not a i have a syntactically valid
email address and that the email address and password are filled in
before i go searching the database to see if the user is valid or not.
Does that make more sense?

The purpose is determine whether or not a i have a syntactically valid
email address and that the email address and password are filled in
before i go searching the database to see if the user is valid or not.
Does that make more sense?

You could use some client-side javascript validation for that.

But if you want to do it server side, you would do something like…

Assuming you params[:email] and params[:password]

u = User.new(params)
u.valid?

email_invalid, password_invalid =
u.errors.on(:email), u.errors.on(:password)

unless email_invalid or password_invalid
user = User.find(:first, :conditions => [‘email = ?’, params[:email]])
blah_blah_blah
end

Dave S. wrote:

The purpose is determine whether or not a i have a syntactically valid
email address and that the email address and password are filled in
before i go searching the database to see if the user is valid or not.
Does that make more sense?

You could use some client-side javascript validation for that.

But if you want to do it server side, you would do something like…

Assuming you params[:email] and params[:password]

u = User.new(params)
u.valid?

email_invalid, password_invalid =
u.errors.on(:email), u.errors.on(:password)

unless email_invalid or password_invalid
user = User.find(:first, :conditions => [‘email = ?’, params[:email]])
blah_blah_blah
end

Dave,

I think this should do the trick. Thank you for your help.

  • J

Jim Dix wrote:

Dave S. wrote:

The purpose is determine whether or not a i have a syntactically valid
email address and that the email address and password are filled in
before i go searching the database to see if the user is valid or not.
Does that make more sense?

You could use some client-side javascript validation for that.

But if you want to do it server side, you would do something like…

Assuming you params[:email] and params[:password]

u = User.new(params)
u.valid?

email_invalid, password_invalid =
u.errors.on(:email), u.errors.on(:password)

unless email_invalid or password_invalid
user = User.find(:first, :conditions => [‘email = ?’, params[:email]])
blah_blah_blah
end

Dave,

I think this should do the trick. Thank you for your help.

  • J

Seem to be having a problem…I added the code you suggested in my login
method and am getting the following error:

NoMethodError in AuthenticationController#login

undefined method `commit=’ for #Member:0x48071b4

The commit field is being generated from the <%= submit_tag “Login” %>
tag. I’m pretty sure I don’t need this in my model, any ideas?

Hmm… I don’t know. (I’m pretty new to this rails thing, also). Are
you using form_for or form_tag?

Can you post the resulting page source, at least for the relevant bits?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs