The correct way to handle invalid form submissions in Rails -
i'm new rails , not sure agree way i've things done in of tutorials i've gone through. issue has how handle invalid form submissions. standard way of doing things seem be:
class thingscontroller < applicationcontroller # post /things def create @thing = thing.new(params[:thing]) if @thing.save flash[:notice] = 'thing created' redirect_to(@thing) else render :action => :new end end
when @thing.save fails, user presented same form, prefilled-out values entered, along flash of went wrong. far good, except url has changed /things/new things/, 1 expect rendering index view instead.
also, if user refreshes page, looking @ index view. if clicks back, prompted resubmit form, i've tried avoid. if redirect_to(new_thing_path), user's previous submission lost, error messages.
i realize restfully, method may "correct", since creation of thing object should result of posting /things, user-interface-wise, don't particularly care it.
i "manually" save invalid @thing object in user's session, displayed after redirect him new_thing_path, feels hack. seems there should "rails way" of doing that.
ideas?
as you've found, default when specify resources :things
, post path creating new thing @ /things
. here's output rake routes
:
things /things(.:format) {:action=>"index", :controller=>"things"} post /things(.:format) {:action=>"create", :controller=>"things"} new_thing /things/new(.:format) {:action=>"new", :controller=>"things"} edit_thing /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"} thing /things/:id(.:format) {:action=>"show", :controller=>"things"} put /things/:id(.:format) {:action=>"update", :controller=>"things"} delete /things/:id(.:format) {:action=>"destroy", :controller=>"things"}
it sounds want more this:
create_things post /things/new(.:format) {:action=>"create", :controller=>"things"} things /things(.:format) {:action=>"index", :controller=>"things"} new_thing /things/new(.:format) {:action=>"new", :controller=>"things"} edit_thing /things/:id/edit(.:format) {:action=>"edit", :controller=>"things"} thing /things/:id(.:format) {:action=>"show", :controller=>"things"} put /things/:id(.:format) {:action=>"update", :controller=>"things"} delete /things/:id(.:format) {:action=>"destroy", :controller=>"things"}
although not recommended, can result following route:
resources :things, :except => [ :create ] post "create" => "things#create", :as => :create, :path => 'new', :on => :collection end
you need modify forms make them post correct path.
all being said, description of urls have in question don't sound right. list following: after submitting new thing
(submitting form @ /things/new
),
- the url changes
/things/new
/things
- clicking prompts resubmit form
- refreshing shows
things#index
this not functionality experience in own rails 3 applications. instead, find that: after submitting new thing
(submitting form @ /things/new
),
- the url changes
/things/new
/things
(this same) - clicking takes user non-submitted form (no request re-post)
- refreshing prompts resubmit form (as expected in opinion)
Comments
Post a Comment