Topic: Rendering "New" View With Validation Errors With Potential for Nesting

Much like the code in the thread here http://railsforum.com/viewtopic.php?id=23294 expect this is the create action (ignoring xml for clarity):

def create
  @result = Result.new(params[:result])
   
  respond_to do |format|
    if @result.save
      flash[:notice] = 'Result created.'
     
      format.html {
        if params[:user_id].blank?
          redirect_to(result_path(@result))
        else
          redirect_to(users_result_path(params[:user_id]))
        end
      }
    else
      format.html {
        render :action => 'new'
      }
    end
  end
end

This works well if @result is saved, but if there are errors, the page is rendered properly but the nested url is lost (/results/new vs /users/:user_id/results/new).  Is there a way to refactor this to retain the url when there are errors?  A redirect_to won't work because the @result object is lost.

Re: Rendering "New" View With Validation Errors With Potential for Nesting

lloks more like the problem described in this thread

and my reply in said thread applies here too:

what does the form_for() look like in the form that's submitted? it has to use the nested route as target. e.g.:
"form_for @result do |f|"
will cause your problem, whereas
"form_for [@user,@result] do |f|"
should work fine.

Re: Rendering "New" View With Validation Errors With Potential for Nesting

What about this:

You have a custom route called "register" which is really just the new action for the user model. If the save fails you can render the new action (but you lose the "register" link) or redirect to the register page (but lose the errors for user)

Can you have both? (register url and errors for user)

Re: Rendering "New" View With Validation Errors With Potential for Nesting

Duplex, just following up, your solution worked, thanks for the info