简体   繁体   中英

Rails 4 with Devise: Validation errors displayed immediately upon rendering form

I'm currently attempting to implement my first non-trivial Rails app and have encountered some problems while trying to implement Devise. Originally, I was using a hand-rolled authentication method as described in the Hartl Rails tutorial. After backing out (or so I believe) all of the hand-rolled code, I added the Devise gem, along with some custom fields, all of which work fine under the covers, meaning I can register, log in, and otherwise use the app.

The problem I have now, however, is that I've missed a trick in validation because as soon as I navigate to the user registration view, the form is rendered complete with big, ugly, red error messages as follows:

Email can't be blank
Password can't be blank
First name can't be blank
Last name can't be blank

Bear in mind that these error messages are generated immediately upon rendering, before the user has even had a chance to enter data.

I can kill the first and last name errors by commenting out the validation in user.rb . If I get rid of the Devise validatable module, I can prevent the email and password errors from showing up.

Here is user.rb:

class User < ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

    belongs_to :entry
    has_many :categories
    has_many :rates
    has_many :entries

    # If I comment out the next four lines regarding validation, error messages disappear

    validates :first_name, presence: true
    validates :first_name, uniqueness: true, if: -> { self.first_name.present? }

    validates :last_name, presence: true
    validates :last_name, uniqueness: true, if: -> { self.last_name.present? }

    def full_name
        fullname = "#{last_name}, #{first_name}"
    end

end

How can I keep these error messages from showing up until the user actually submits data, and when the data is submitted, how can I validate each field (seeing as my current non-solution is just to avoid validation altogether)? Thanks in advance.

Some bad news and some good news. The bad news is that I'm half bald after tearing my hair out over this issue. The good news is there's an easy solution.

The problem had nothing to do with the User model or validation. The problem was the way I was linking to the registration mechanism.

I have an index page with links to register, sign up and sign out. The problem was the way I constructed the link for registration.

Wrong:

<%= link_to "Register", user_registration_path, :method => :post %>

By specifying the POST method, I was telling Rails to take the data and attempt to register the user when someone clicked the link. Of course, when you're only trying to render the registration form, there is no data . The params hash is empty, so it would throw errors to the form based on both the :validatable module that I included (which only validates the barebones devise fields of :email and :password ) and the custom validations that I added for :first_name and :last_name .

I can't believe I couldn't see the forest for the trees. POST means "modify this," but my intention was just to render a form, which means GET would have been appropriate. The POST comes after the user supplies data, and is properly tied to the Submit button, not the link to the form.

When you change the method, you also have to change the route to match, since POST is associated with the route devise/registrations#create .

Right:

<%= link_to "Register", new_user_registration_path, :method => :get %>

I fell into this trap because I believed that I had backed out all the original authentication code I implemented prior to adopting Devise, which was true for the models, controllers, and routes, but not true for the views.

So now I have to post to Hair Underflow about how to get my hair to grow back faster, but in the meantime, I am relieved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM