Suppose I have something like this:
def new @user = User.new end def create @user = User.create(params[:user]) if @user.save flash[:notice] = 'User created' redirect_to :action => 'list' else flash[:error] = 'Some error here!' render 'new' end end
I think the code is clear.The problem here is that if the object was @usernot saved successfully, should I do new(as above) or redirect to new?
@user
new
I know if redirecting to newlost data that the user will lose, but if I draw new, the URL will be /users/createinstead /users/new(which is ugly!).
/users/create
/users/new
Correctly you do not use redirect. Redirection loads a completely new resource.
redirect
render , , , .
render
:
, , , , URL /users/create /users/new ( !).
, . render 'new', URL users/new . POST , , . new .
render 'new'
users/new
POST
create update, , PUT, edit .
update
PUT
edit