How do I maintain the same controller & action in a page URL upon re-rendering an action in Rails?

This is actually the intended behavior for this process. In a standard scaffolded RESTful controller, a validation error in the create and update actions will simply render the original template without redirecting. This results in what you are seeing – the new template will be displayed with the create action's URL in the URL bar.

The reason for this is that in order to display information to the user about what errors occurred, the view must have access to the invalid model object, which is user_session in your case You can use redirect_to instead of render if you want to force a redirect to the original URL, but this will cause you to lose information about the errors. You would need to manually persist the errors in the session, which would be messy. My advice is not to worry about the fact that the URL doesn't match that of the original as this is pretty standard in all Rails apps.

This is actually the intended behavior for this process. In a standard scaffolded RESTful controller, a validation error in the create and update actions will simply render the original template without redirecting. This results in what you are seeing – the new template will be displayed with the create action's URL in the URL bar.

The reason for this is that in order to display information to the user about what errors occurred, the view must have access to the invalid model object, which is @user_session in your case. You can use redirect_to instead of render if you want to force a redirect to the original URL, but this will cause you to lose information about the errors. You would need to manually persist the errors in the session, which would be messy.My advice is not to worry about the fact that the URL doesn't match that of the original as this is pretty standard in all Rails apps.

I appreciate the suggestion, but I find the forced inconsistency unsettling. Anyone getting errors on a page and wanting to report it will end up linking to a page whose link makes no sense when clicked. Using a redirect_to and persisting the errors is, as you say, messy as well.

– Shaun Dec 5 '10 at 16:01 There isn't really another choice. The data has to be persisted somehow. Either you don't do a redirect, or you store it somewhere and redirect.

– Jimmy Cuadra Dec 7 '10 at 20:06 I found an easy way to accomplish my goal without having to complicate my controllers and views in this manner. See accepted answer. – Shaun Dec 12 '10 at 2:23.

After further digging, I found the solution in another StackOverflow question: Use custom route upon model validation failure I simply modified my routes to add a new one for posing to '/account/login': map. Login '/account/login', :controller => :user_sessions, :action => :new, :conditions => {:method => :get} map. Login_post '/account/login', :controller => :user_sessions, :action => :create, :conditions => {:method => :post} Then, I updated my view to utilize the new route: login_post_path do |f| %> This works perfectly.

A failed login gives the appropriate error messages and maintains the '/account/login' URL.

– madebylam Sep 10 at 0:19 nevermind, using :via! – madebylam Sep 10 at 0:30.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions