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! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, 3:43 pm, Scott Parks <scottapa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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? On Aug 7, 4:02 pm, Taro <japtar10...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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, 3:43 pm, Scott Parks <scottapa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
[Please don''t top post as it makes the discussion more difficult to follow.] On Aug 7, 2007, at 15:20 , Scott Parks wrote:> 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?Why are you raising an error? Just return true or false if the user authenticates or not. Michael Glaesemann grzm seespotcode net --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
IMHO you should refresh your knowledge about MVC (model-view- controller) design pattern. http://en.wikipedia.org/wiki/Model-View-Controller · 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 On Aug 7, 9:43 pm, Scott Parks <scottapa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Embrace MVC! Fighting this will frustrate you greatly. On Aug 7, 4:33 pm, dima <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> IMHO you should refresh your knowledge about MVC (model-view- > controller) design pattern.http://en.wikipedia.org/wiki/Model-View-Controller > > · 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 > > On Aug 7, 9:43 pm, Scott Parks <scottapa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Aug 8, 7:39 pm, Robert Walker <rwalker...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Embrace MVC! Fighting this will frustrate you greatly. > > On Aug 7, 4:33 pm, dima <dejan.di...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > IMHO you should refresh your knowledge about MVC (model-view- > > controller) design pattern.http://en.wikipedia.org/wiki/Model-View-Controller > > > · 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 > > > On Aug 7, 9:43 pm, Scott Parks <scottapa...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > > > Thanks!--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---