James Whittaker
2006-Apr-28 10:59 UTC
[Rails] Handling errors - incorrect value entered in url...
I have many "edit" actions in my project. If the url is like http://localhost/project/3/edit and someone enters http://localhost/project/3333333/edit the edit action will fails because it cannot find a project with id of 3333333. To combat this I am adding to every action like edit: rescue redirect_to :action => ''index'' flash[''errors''] = "That project cannot be edited." This works, however is there a way to do this globally or do I have to entered this for every action that could error in this way? I understand about using routes to narrow down what URL''s can be typed, but this does not escape an error when a value entered is incorrect. Incidentally, I looked at the Typo code and could not see any error checking, not a rescue in sight. How do they do it in typo? -- Posted via http://www.ruby-forum.com/.
Robby Russell
2006-Apr-28 13:11 UTC
[Rails] Handling errors - incorrect value entered in url...
On Apr 28, 2006, at 3:59 AM, James Whittaker wrote:> I have many "edit" actions in my project. If the url is like > http://localhost/project/3/edit and someone enters > http://localhost/project/3333333/edit the edit action will fails > because > it cannot find a project with id of 3333333. > > To combat this I am adding to every action like edit: > > rescue > redirect_to :action => ''index'' > flash[''errors''] = "That project cannot be edited." > This works, however is there a way to do this globally or do I have to > entered this for every action that could error in this way? I > understand > about using routes to narrow down what URL''s can be typed, but this > does > not escape an error when a value entered is incorrect.You could have a before_filter call a function that tests that the id in your URL is valid. If you also want to verify that the right person is attempting to edit it.. you could do that there as well. Hope that helps. Cheers, -Robby Robby Russell Founder & Executive Director PLANET ARGON, LLC Ruby on Rails Development, Consulting & Hosting www.planetargon.com www.robbyonrails.com +1 503 445 2457 +1 877 55 ARGON [toll free] +1 815 642 4968 [fax]
Michael Gorsuch
2006-Apr-28 13:16 UTC
[Rails] Handling errors - incorrect value entered in url...
James, take a look at this: http://rubyonrails.org/api/classes/ActionController/Rescue.html#M000069 Implement the ''rescue_action_in_public'' method in your controller (or in application.rb) to define behavior based on a common exception. You could display a 404, or whatever you want. On 4/28/06, Robby Russell <robby.lists@planetargon.com> wrote:> > > On Apr 28, 2006, at 3:59 AM, James Whittaker wrote: > > > I have many "edit" actions in my project. If the url is like > > http://localhost/project/3/edit and someone enters > > http://localhost/project/3333333/edit the edit action will fails > > because > > it cannot find a project with id of 3333333. > > > > To combat this I am adding to every action like edit: > > > > rescue > > redirect_to :action => ''index'' > > flash[''errors''] = "That project cannot be edited." > > This works, however is there a way to do this globally or do I have to > > entered this for every action that could error in this way? I > > understand > > about using routes to narrow down what URL''s can be typed, but this > > does > > not escape an error when a value entered is incorrect. > > You could have a before_filter call a function that tests that the id > in your URL is valid. If you also want to verify that the right > person is attempting to edit it.. you could do that there as well. > > Hope that helps. > > Cheers, > > -Robby > > > Robby Russell > Founder & Executive Director > > PLANET ARGON, LLC > Ruby on Rails Development, Consulting & Hosting > > www.planetargon.com > www.robbyonrails.com > > +1 503 445 2457 > +1 877 55 ARGON [toll free] > +1 815 642 4968 [fax] > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060428/0e23e6f8/attachment.html
Michael Gorsuch
2006-Apr-28 13:18 UTC
[Rails] Handling errors - incorrect value entered in url...
Also check out the wonderful exception_notification plugin for ideas as to how you can use this to better suit your needs. On 4/28/06, Michael Gorsuch <michael.gorsuch@gmail.com> wrote:> > James, take a look at this: > > http://rubyonrails.org/api/classes/ActionController/Rescue.html#M000069 > > Implement the ''rescue_action_in_public'' method in your controller (or in > application.rb) to define behavior based on a common exception. You could > display a 404, or whatever you want. > > > On 4/28/06, Robby Russell < robby.lists@planetargon.com> wrote: > > > > > > On Apr 28, 2006, at 3:59 AM, James Whittaker wrote: > > > > > I have many "edit" actions in my project. If the url is like > > > http://localhost/project/3/edit and someone enters > > > http://localhost/project/3333333/edit the edit action will fails > > > because > > > it cannot find a project with id of 3333333. > > > > > > To combat this I am adding to every action like edit: > > > > > > rescue > > > redirect_to :action => ''index'' > > > flash[''errors''] = "That project cannot be edited." > > > This works, however is there a way to do this globally or do I have to > > > entered this for every action that could error in this way? I > > > understand > > > about using routes to narrow down what URL''s can be typed, but this > > > does > > > not escape an error when a value entered is incorrect. > > > > You could have a before_filter call a function that tests that the id > > in your URL is valid. If you also want to verify that the right > > person is attempting to edit it.. you could do that there as well. > > > > Hope that helps. > > > > Cheers, > > > > -Robby > > > > > > Robby Russell > > Founder & Executive Director > > > > PLANET ARGON, LLC > > Ruby on Rails Development, Consulting & Hosting > > > > www.planetargon.com > > www.robbyonrails.com > > > > +1 503 445 2457 > > +1 877 55 ARGON [toll free] > > +1 815 642 4968 [fax] > > > > > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060428/e1ef3a99/attachment-0001.html