The environment is as follows. Mac OS Catalina 10.15.7 Ruby 2.7.1 Rails 22.214.171.124
I will try to record it as easily as possible, also as a memorandum of my own. To illustrate the problem, we'll use the Rails tutorial as an example.
Specifically, when implementing the user registration function in Chapter 7, the URL will change from
/ signup to
/ users when it is rendered on the form screen after registration failure due to validation. The problem, that is
URL changes from
localhost: 3000 / signup to
localhost: 3000 / users
→ An error occurs when reloading because the view corresponding to users is not created.
I will explain the solution to the problem.
When the user registration fails in the create action,
render'new' means that the new action is not called and only the view is switched.
Since the create action is / users, the URL will naturally be
/ users after the action.
The inconsistency between this URL and the screen is a problem.
/ users to
/ signup, that is, it looks like the URL is fixed as
/ signup even after the action.)
history.replaceState('', '', '/signup')
I am editing the current history with the history.replaceState () method and replacing
/ users with
/ signup with the URL parameters passed to the method.
//↓ Original code require("@rails/ujs").start() require("turbolinks").start() require("@rails/activestorage").start() require("channels") //↓ Add this require("users/signup_render")
ʻIf @ user.errors.any?` sets the condition "Validation error occurs = Please process js only when user registration fails and renders."
I hope you find this helpful.