Redirect from a model

What is the best way to call a redirect from a model? Using the Rails
Recipes book I have the authentication working and adding conditions
if someone submits the form blank, but in the book they have a
raise “Invalid Login Or Password”.

Instead I would like to do a flash and redirect back the sign in from.

Can I call a redirect from the model?

Thanks!

I’m fairly new to Rails, but from the limited experience I have, and
from looking at the Rails Documentation, I believe you cannot call
redirect or render from a model.

This is actually not much of a problem, though. If the validation
fails, either save or update method will return false; use this to
your advantage.
So, if you’re trying to edit something, write something like this in
the controller:

def edit
if Model.save()
flash[:notice]=‘Success!’
#redirect
else
render :action=>‘edit’
end
end

On Aug 7, 2007, at 14:43 , Scott Parks wrote:

What is the best way to call a redirect from a model? Using the Rails
Recipes book I have the authentication working and adding conditions
if someone submits the form blank, but in the book they have a
raise “Invalid Login Or Password”.

Instead I would like to do a flash and redirect back the sign in from.

Can I call a redirect from the model?

I don’t know if you can, but I do know you shouldn’t. This is the
province of the controller. The controller should decide its action
based on the authentication result. Simply put: the model decides the
authentication, and the controller decides what to do with the result
of the authentication.

Michael G.
grzm seespotcode net

I am not doing a save or update, I am simply authenticating the user,
here is my code:

In my Institution model I have

def self.authenticate(login_email, password)
institution = Institution.find(:first, :conditions =>
[‘login_email = ?’, login_email])
if institution.blank? || Digest::SHA256.hexdigest(password +
institution.password_salt) != institution.password_hash
raise “Login Email or Password invalid”
end
institution
end

You can see the raise, but is there a way I can check that in the
controller and render if it fails?

[Please don’t top post as it makes the discussion more difficult to
follow.]

On Aug 7, 2007, at 15:20 , Scott Parks wrote:

You can see the raise, but is there a way I can check that in the
controller and render if it fails?

Why are you raising an error? Just return true or false if the user
authenticates or not.

Michael G.
grzm seespotcode net

Embrace MVC! Fighting this will frustrate you greatly.

IMHO you should refresh your knowledge about MVC (model-view-
controller) design pattern.

· Model, represents the underlying, logical structure of data and the
high-level class associated with it.
· View, is a collection of classes representing the elements in the
user interface
· Controller, represents the classes connecting the model and the
view, and is used to communicate between classes in the model and
view.

In any case your redirection should be done from the controller

Hi,

I’m also fairly new to rails. I’ve used the acts_as_authenticated
plugin for that purpose. It works very well for me.
In general your authentication should not happen in the model but in
the controller.
If you don’t wanna use acts_as_autenticated I think you could do the
following:
Use a before_filter in your controller that will then call your
authentication method (in the controller) which either checks just
checks the database directly to see if the login data is valid or
which will do something like
user = user.load(username, password). The load function would then
return the user object or false if the login data was not correct. If
false is returned your authentication method (in the controller)
should then do the redirection.

In general, your model’s methods should never do anything like that.
Their only purpose is to check input data and add, update or delete
records and return a boolean value or an object to the controller
method that called the model method.