Peter Michaux
2006-Jul-03 03:24 UTC
[Rails] REST vs. render vs. redirect_to w.r.t. create error messages, efficiency and the desired URL
Hi, Something has been bothering me for a long time and I''d like to know what other think about this common situation with Rails. Someone is looking at http://example.com/user/new where there is a form that POSTs the data to the user/create action. The controller might look like def new @user = User.new end def create @user = User.new(params[:entry]) if @user.save flash[:notice] = "User was successfully created" redirect_to :action => "list" else render :action => ''new'' end end If the create action recieves invalid data and the ".save" fails then the the form will be redisplayed with the error messages just like we want. But the URL will be http://example.com/user/create which is wrong. If this address is bookmarked in the browser, the bookmark won''t work as expected if used. The user would probably expect to see the empty form but will see a form of errors or be redirect to the list view if verify POST is being used for the create action. Another option is to use a redirect_to :action => new instead of render but this requires an extra back and forth to the browser and the data and error messages won''t be displayed. But the URL will be correct. Another option is to have the create logic inside the new action. This works but the code is not very modular. Now I see that DHH has proposed on one of his "World of Resources" slides using actions like the following # GET /people;new def new() end # POST /people def create() end If the create fails what will the URL be in the browser? How do you handle this URL issue? Thanks, Peter