Ask A Question

Notifications

You’re not receiving notifications from this thread.

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.

Reply

If you're finding that you don't get 'Thanks!' back after setting up your create method, add local: true to your form.

Reply

Thanks for pointing that out, local: true resolves it

Reply

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!

Reply

I could not use @user.errors.any? in rails console.

Reply

but if I use user=@user.errors.any? Maybe it is my ruby version it is not the 3 but the 2.7... but my rails is 6.1.1 .
The error is in minute 6:37 <% if user = @user.errors.any? %>

Reply

Very sorry my wrong here. I did not saved the data bank so it was empty. Very sorry.

Reply

hi, when I click sign up nothing happens, why :( ?

Reply

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

Reply

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.

Reply

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

Reply

How can I translate the error message?

Reply

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 )

Reply

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

Reply

Hotwire turbo was causing the problems for me.
adding 'data: { turbo: false }' to the form.submit made the form work like it should.

Reply

Adding 'data: { turbo: false }' & setting "local: true" worked for me

Reply

fixed the issue for me. thank you!

Reply

Thanks guys, it worked for me too.

Reply

Thanks all!

Reply

Same here - thank you!

Reply

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??

Reply

I figured this out. In my form, i had @User while it was declared as @user

Reply

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| %>

Reply

Thank you buddy this really saved my day

Reply

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.

Reply

Thank you Michael

Reply

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| %>

Reply

Thanks!!

Reply

Thank you Michael!

Reply

It is very useful. Thank you Michael!

Reply

Thanks for that!

Reply

Thanks! This worked for me, as well!

Reply

Adding data: {turbo: false} solved my issue. Thanks!

Reply

Thanks Mike, your feedback is very valuable

Reply

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
Reply

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
Reply

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 : )

Reply

These videos are a little too outdated for rails 7, which is causing a bunch of issues trying to follow along :(

Reply

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

Reply

Handling sign-up errors in Rails involves providing informative feedback to users when issues occur during the registration process.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 86,946+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.