user3190923
user3190923

Reputation: 23

devise undefined method `first_name'

So, I'm using devise for User authentification for a rails app. I wish to add First Name and Last Name to the devise users. So, I created new columns to the Users database. Unfortunately, I'm getting "undefined method `first_name' " when I try my form. Here is the form:

        <h2>Sign up</h2>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <div><%= f.label :first_name %><br />
      <%= f.first_name :first_name, autofocus: true %></div>

    <div><%= f.label :last_name %><br />
      <%= f.last_name :last_name, autofocus: true %></div>

    <div><%= f.label :email %><br />
    <%= f.email_field :email, autofocus: true %></div>

    <div><%= f.label :password %><br />
      <%= f.password_field :password, autocomplete: "off" %></div>

    <div><%= f.label :password_confirmation %><br />
      <%= f.password_field :password_confirmation, autocomplete: "off" %></div>

    <div><%= f.submit "Sign up" %></div>
<% end %>

I confirmed that the database columns were actually added by using the console. I typed

user = User.new into the console and got the following:

    "=> #<User id: nil, email: "", encrypted_password: "", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, first_name: nil, last_name: nil> "

When I type user.first_name, the console returns nil (which is good, as no errors are returned, so the columns were indeed added to the database). What is going on? What are possible fixes?

Upvotes: 2

Views: 880

Answers (1)

Pavan
Pavan

Reputation: 33542

undefined method 'first_name'

In your case,the error is due to this line

<%= f.first_name :first_name, autofocus: true %>

This is supposed to be like this

<%= f.text_field :first_name, autofocus: true %>

And also this line

<%= f.last_name :last_name, autofocus: true %>

has to be changed to the same to avoid another error

<%= f.text_field :last_name, autofocus: true %>

You are using attribute names instead of text_field,so is the error.

Have a look at these Guides for the available form_helpers and their usage.It would be very useful.

Upvotes: 7

Related Questions