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.
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
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.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.