Stay on the same route if form submission failed #132
-
Does anyone know how to let the client stay on the same route if a form submission failed? Supposed I am in route "users/new". Upon submission, the route goes to "/users" (as specified in the action attribute) even if the form has failed to submit. What I have been doing is changing the route for create method to [ex. POST '/users/new'] so the form will be submitted to this path. Even if it has failed, it will still be on the same route; but, this method is not right if I have to submit a form to a remote location. I could just force redirection to the current page if the form failed to submit, but I could not think of any way to capture the valid credentials that were already entered (Rails doesn't allow redirection and rendering at the same time with one action). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
devise_for :users, To override the above route, custom routes were created for user registration, so the form will submit to the current route. |
Beta Was this translation helpful? Give feedback.
devise_for :users,
controllers: { registrations: 'users/registrations'}
The above code creates one of the routes as POST "/users" for create_user. When the for submits even if it fails, the page will redirect to this route.
To override the above route, custom routes were created for user registration, so the form will submit to the current route.
devise_scope :user do
post 'users/sign_up' => 'users/registrations#create', as: 'user_registration'
get 'users/sign_up' => 'users/registrations#new', as: 'new_user_registration'
end