Rails for Beginners Part 14: Handling Sign Up Errors Discussion
Love these Chris, noting some new tricks along the way. Plus your always positive demeanour makes this so enjoyable to go through.
If you're finding that you don't get 'Thanks!' back after setting up your create method, add local: true
to your form.
Hey! Where in the form are you adding the local: true, using sublime 3 and have tried plugging it in a few places? thank you!
I could not use @user.errors.any? in rails console.
hi, when I click sign up nothing happens, why :( ?
I am just learning Ruby on Rails now, I am no expert, but what worked for me was adding "local: true" to the form and
@user = User.new(user_params)
if @user.save
redirect_to root_path, notice: "Successfully created account"
else
render :new, status: 422
end
in the controller
Solution please!!!
when i create the account with already existing email ,, then it does not give an error.
already existing email validation is not working.
Currently we're only validating that each user instance has an email but we don't specify whether it should be unique or not:
https://guides.rubyonrails.org/active_record_validations.html#uniqueness
As others have pointed out, https://discuss.rubyonrails.org/t/rails-6-1-remote-forms-are-no-longer-default/76912 is a point of discussion with the difference on Rails 6.1 vs 6.0 (which i was using )
I'm also having the issue that the new view does not show the errors.
Render :new is called and @user.errors does have the correct errors.
Using Rails 6.1.3.1
Hotwire turbo was causing the problems for me.
adding 'data: { turbo: false }' to the form.submit made the form work like it should.
I'm getting an error that I can't seem to get past.
When submitting the form, I get:
'ActionController::ParameterMissing (param is missing or the value is empty: user'
I feel like something isn't correctly structured somewhere. Here is the output of my Params:
Parameters: {"authenticity_token"=>"[FILTERED]", "email"=>"travs", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "commit"=>"Create User"}
.
Unlike in the video, my email, password, and password_confirmation are NOT wrapped insider a user param. Where does that happen??
Incase you are having an issue with the sign up button not working use this:
<%= form.submit "Sign Up", data: { turbo: false }, class: "btn btn-primary"%>
and on your form_with add local:true like this:
<%= form_with model: @user, url: sign_up_path, local: true do |form| %>
For those who are using Rails 7:
Rails 7 has Hotwire enabled by default, and it will validate the response status code. For failed validation, the status code should be 422 (Unprocesssable entity). Otherwise, Hotwire may believe the form is valid, hence it doesn't work.
So in you registrations_controller create method, instead of
else
render :new
end
add a status option
else
render :new, status: :unprocessable_entity
end
This way Hotwire knows "Hey, the form is invalid", and the validation shall work.
Thanks Michael! Wish I read your comment before searching SO :D
To read more on Michael's answer https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission
Another soln:
<%= form_with model: @user, data: { turbo: false }, url: sign_up_path do |form| %>
I was getting this error in my dev tools Error: Form responses must redirect to another location
anytime I hit the form submit button. However, Michael's comment above helped me sort it out. I needed to add status: :unprocessable_entity
to my registrations_controller.rb
file. The full code looks like this:
class RegistrationsController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
session[:user_id] = @user.id
redirect_to root_path, notice: "Thank you for signing up!"
else
render :new, status: :unprocessable_entity
end
end
private
def user_params
params.require(:user).permit(:email, :password, :password_confirmation)
end
end
As of now, June 10th 2022, the error message does not show. I had to add status: :unprocessable_entity
in order for the error to show.
def create
@user = User.new(user_params)
if @user.save
redirect_to root_path, notice: "Successfully created account"
else
render :new, status: :unprocessable_entity
end
end
Using Rails 7: I was able to get "Thanks!" and params[:user]
to render using the changes that Martin suggested: (Thank you Martin! : ) )
<%= form.submit "Sign Up", data: { turbo: false }, class: "btn btn-primary"%>
and on your form_with add local:true like this:
<%= form_with model: @user, url: sign_up_path, local: true do |form| %>
I had to restart the rails server after saving the changes for it to work : )
Of course, the point is to get the user registration to write to the database but it's nice to follow along and get the same results that Chris is getting : )
These videos are a little too outdated for rails 7, which is causing a bunch of issues trying to follow along :(
Rails 7 Users can fix the problem with this:
<%= form.submit "Sign Up",data: { turbo_frame: 'signup_frame'} %> for signup
and <%= button_to "Logout", logout_path, method: :delete, data: { turbo_frame: 'logout_frame', turbo_confirm: 'Are you sure?' } for logout
Handling sign-up errors in Rails involves providing informative feedback to users when issues occur during the registration process.