I’m currently working my way through various Rails tutorials, but all seem to recommend that a failed form submission should use a render to output the form with the errors. For example, the create method looks like this:
def create
@user = User.new(params[:user])
if @user.save
flash[:success] = "Welcome to the Sample App"
redirect_to @user
else
@title = "Sign up"
render 'new'
end
end
This has the desired effect, but means that if I have a new user form at http://localhost:3000/users/new and an error occurs, I get the same form with errors at http://localhost:3000/users
Is there a way to make sure I go to http://localhost:3000/users/new instead?
I thought about using a redirect instead of render ‘new’, but this would cause problems with displaying the form errors. Someone on the Rails IRC directed me to a way of doing clientside validations, but that seems like it dodges the issue.
I had a similar problem with my user signup form. Since I do not use RESTful routes there, I was able to solve it with the following routes:
and in the new form: