Ask A Question

Notifications

You’re not receiving notifications from this thread.

Rails for Beginners Part 17: Login Form Discussion

It would be great to show how to do login with both username and email as well.

Reply

mm... is the same with, just create the username in your model, and validate username instead of email.

Reply

Yes I know. I meant be able to login with either at the same time, similar to how you do it with Devise. https://github.com/heartcombo/devise/wiki/How-To:-Allow-users-to-sign-in-using-their-username-or-email-address

Reply

I'm getting this error when I try to login:

BCrypt::Errors::InvalidHash in SessionsController#create

I copied the sessions_controller.rb file from the source code. I also created a new user successfully, but it did not fix the issue.

Reply

Probably because you created your user before you added has_secure_password. Try to delete the user from DB and create a new one and then try to log in.

Reply

My sign_in page button is not working (I am not getting any flash messages). At first I thought it might have been related to Lesson 14, which I resolved by adding status: :unprocessable_entity (see my comment at at https://gorails.com/episodes/rails-for-beginners-part-14-handling-sign-up-errors) but that did not fix it. Here is my code (fyi - I am using TailwindsCSS instead of Bootstrap):

<div class='max-w-6xl mx-auto flex flex-col'>
    <h1 class='font-semibold text-2xl text-center mb-4'>Sign In</h1>

    <%= form_with url: sign_in_path, local: true do |form| %>
        <form class="w-full max-w-sm mb-4">
            <div class="md:flex md:items-center mb-6">
                <div class="md:w-1/3">
                <label class="block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4" for="inline-full-name">
                    Email
                </label>
                </div>
                <div class="md:w-2/3">
                <input class="bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500" id="inline-full-name" type="text" value="Jane Doe">
                </div>
            </div>
            <div class="md:flex md:items-center mb-6">
                <div class="md:w-1/3">
                <label class="block text-gray-500 font-bold md:text-right mb-1 md:mb-0 pr-4" for="inline-password">
                    Password
                </label>
                </div>
                <div class="md:w-2/3">
                <input class="bg-gray-200 appearance-none border-2 border-gray-200 rounded w-full py-2 px-4 text-gray-700 leading-tight focus:outline-none focus:bg-white focus:border-purple-500" id="inline-password" type="password" placeholder="******************">
                </div>
            </div>
            <div class="md:flex md:items-center">
                <div class="md:w-1/3"></div>
                <div class="md:w-2/3 mb-4">
                    <div class="text-center shadow bg-indigo-500 hover:bg-purple-400 focus:shadow-outline focus:outline-none text-white font-bold py-2 px-4 rounded" type="button">
                        <%= form.submit "Sign In" %>
                    </div>
                </div>
            </div>
        </form>
    <% end %>
</div>

As an aside I am getting this error in the dev tools too:

Uncaught TypeError: Error resolving module specifier “application”. Relative module specifiers must start with “./”, “../” or “/”.

I am not sure what is "breaking" my Sign In" button, any pointers on resolving would be greatly appreciated. Cheers

Reply

The problem was on line 11. It was flash.now.alert = "Invalid email or password." but it should have been flash[:alert] = "Invalid email or password" strike 112 for GitHub CoPilot

Reply

I ran into this error too (Ruby 3.1.0 and Rails 7.0.2.2). If you change render :new to render: new, status: :unprocessable_entity, it works as described in this video.

Reply

I love how concerned Chrome is about the lack of security of Chris' example password haha

Reply
Join the discussion
Create an account Log in

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

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

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