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
actio开发者_开发百科n with setting the URL path as /edit
instead of posts/1
.
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
else
session[:post] = @post
redirect_to post_edit(@post)
end
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?
精彩评论