How should I handle redirecting to pages from the model? Basically I have credit cards that get processed in a model. If successful, I want the user to be redirected to a certain page, of unsuccessful I would like them redirected to a new page. Obviously if I try and do this in the model, I get a undefined method `redirect_to'' error. How should I handle this? a method in my model: def authorize_payment if creditcard.valid? response = gateway.authorize(100, creditcard, options) print "(TEST) " if response.test? if response.success? puts "The transaction was successful! The authorization is #{response.authorization}" redirect_to :controller => ''/account'', :action => ''index'' flash[:notice] = "Thanks for signing up!" else puts "The transaction was unsuccessful because #{response.message}" redirect_to :controller => ''/account/'', :action => ''signup_unlimited'' flash[:notice] = "There was a problem with your credit card, please verify your information and try again." end else puts "The credit card is invalid" end end end --~--~---------~--~----~------------~-------~--~----~ 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 Feb 26, 5:18 pm, Keaja <jasonmw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How should I handle redirecting to pages from the model? > > Basically I have credit cards that get processed in a model. If > successful, I want the user to be redirected to a certain page, of > unsuccessful I would like them redirected to a new page. > > Obviously if I try and do this in the model, I get a undefined method > `redirect_to'' error. How should I handle this? > > a method in my model: > > def authorize_payment > if creditcard.valid? > response = gateway.authorize(100, creditcard, options) > print "(TEST) " if response.test? > if response.success? > puts "The transaction was successful! The authorization is > #{response.authorization}" > redirect_to :controller => ''/account'', :action => ''index'' > flash[:notice] = "Thanks for signing up!" > else > puts "The transaction was unsuccessful because > #{response.message}" > redirect_to :controller => ''/account/'', :action => > ''signup_unlimited'' > flash[:notice] = "There was a problem with your credit card, > please verify your information and try again." > end > else > puts "The credit card is invalid" > end > end > endglad i can help there buddy if authorize_payment redirect_to :action => "x" else redirect_to :action => "y" end You see, the model will throw false if it is not successful. You should never do logic in your model. NEVER --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
That logic should go in a controller. The model should perform can perform the validation and authorization but the result should be returned to the controller to execute the business logic. On Feb 26, 5:18 pm, Keaja <jasonmw...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How should I handle redirecting to pages from the model? > > Basically I have credit cards that get processed in a model. If > successful, I want the user to be redirected to a certain page, of > unsuccessful I would like them redirected to a new page. > > Obviously if I try and do this in the model, I get a undefined method > `redirect_to'' error. How should I handle this? > > a method in my model: > > def authorize_payment > if creditcard.valid? > response = gateway.authorize(100, creditcard, options) > print "(TEST) " if response.test? > if response.success? > puts "The transaction was successful! The authorization is > #{response.authorization}" > redirect_to :controller => ''/account'', :action => ''index'' > flash[:notice] = "Thanks for signing up!" > else > puts "The transaction was unsuccessful because > #{response.message}" > redirect_to :controller => ''/account/'', :action => > ''signup_unlimited'' > flash[:notice] = "There was a problem with your credit card, > please verify your information and try again." > end > else > puts "The credit card is invalid" > end > end > end--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> glad i can help there buddy > > if authorize_payment > redirect_to :action => "x" > else > redirect_to :action => "y" > end > > You see, the model will throw false if it is not successful. You > sould never do logic in your model. NEVERAlso from my experience, not sure if it can be done another way, i would declare the model function as self.authorze_payment that way, if the model is called, "payment", from the controller you can call payment.authorize_payment. Inject that into the if statement i gave you earlier and voila, perfect execution --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks guys, alot of really useful information. Everytime I think I am getting a handle on ruby, I realize how much I still have to learn haha =) On Feb 26, 2:34 pm, nobility <nobil...-WYY/XuMTj9yq9X8JwYP7BtBPR1lH4CV8@public.gmane.org> wrote:> > glad i can help there buddy > > > if authorize_payment > > redirect_to :action => "x" > > else > > redirect_to :action => "y" > > end > > > You see, the model will throw false if it is not successful. You > > sould never do logic in your model. NEVER > > Also from my experience, not sure if it can be done another way, i > would declare the model function as > > self.authorze_payment > > that way, if the model is called, "payment", from the controller you > can call payment.authorize_payment. Inject that into the if statement > i gave you earlier and voila, perfect execution--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---