Hi -- On Sun, 30 Nov 2008, Nick Hoffman wrote:
> > The usual CRUD controller actions assume that certain parameters were > sent. Let's use a controller for books as an example: > > BooksController < ApplicationController > def create > @book = Book.new params[:book] > # ..etc.. > end > end > > So #create assumes that params[:book] exists. This is fine, because if > the "book" field wasn't POSTed to #create, then params[:book] will > simply be nil, because 'params' is a Hash. > > However, if our controller action tries to look at params[:book] > [:page_ids] when the "book" field wasn't POSTed to #create, we'd get a > NoMethodError. This is because params[:book] is nil, which means that > params[:book][:page_ids] is equivalent to nil[:page_ids] , which will > obviously raise an error. > > So the question is, do you bother to write additional checks for your > controller actions for these edge cases? > > I would, because I like to be thorough and know that I've covered all > of my bases. But I can understand why people wouldn't bother, because > said edge cases will probably only be generated by yourself during > testing, and by hooligans mucking with your forms. > > What do you guys think? One good way to think about it is: if you did have an extra check, what would it do? In other words, if create gets executed and params[:book] is nil, what would you want to happen? And, as a follow-up, does your current error-reporting process already do what you would want to have done in such a case? David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS (Jan 12-15), Fort Lauderdale, FL See http://www.rubypal.com for details Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

