Take the 2-minute tour ×

In my Rails3 application I add a specific match for edit my Post model as:

match '/edit' => 'posts#edit', :as => 'post_edit'

So, I overwrite the default path of the edit action. - Now if any errors occurs in the update action, it will render the edit action with settting the URL path with the default edit path /posts/1.

How can I overwrite that to render the edit action with setting the URL path as /edit instead of posts/1.

share|improve this question
add comment

1 Answer

You are actually seeing the URL for "update", the default path for edit would be /posts/1/edit. I don't think you will be able to change what the URL displays using render :action. An alternate, although somewhat sloppy, method would be to redirect and save the @post object in the session or flash. If you do not save the @post object, you will lose the error messages from the update.

if @post.update_attributes(params[:post])
  #business as usual
  session[:post] = @post
  redirect_to post_edit(@post)

Note that it is bad practice to save the whole object in the session (especially large objects), so you may instead want to only send the error message string with the flash and render that in the view. These are both rather hackish methods, but I don't really see an elegant way to do this.

Out of curiosity, why do you want to change the default URL? Is it necessary?

share|improve this answer
Actually, the url would /posts/1/edit –  nathanvda Dec 9 '10 at 20:31
actually, you are correct ;-) Thanks for catching that! answer updated –  cowboycoded Dec 9 '10 at 20:43
add comment

Your Answer


