0
votes

I'm trying to add the field "name" to Devise, but when I sign up a new user it breaks when I push submit.

I've made sure to run rake db:migrate and I've restarted my server, but I get this error:

ActiveRecord::StatementInvalid in Devise::RegistrationsController#create

NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "users" ("created_at", "current_sign_in_at", "current_sign_in_ip", "email", "encrypted_password", "last_sign_in_at", "last_sign_in_ip", "name", "pic_file_name", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING "id"

User Class

  class User < ActiveRecord::Base
  has_and_belongs_to_many :roles

    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

    attr_accessible :email, :password, :password_confirmation, :remember_me, :role_ids, :name, :title, :pic_file_name
    has_one :picture, :dependent => :destroy
    accepts_nested_attributes_for :picture, :allow_destroy => true
    has_many :comments, :dependent => :destroy
    has_many :discussions, :dependent => :destroy



    def role?(role)
      roles.map(&:name).include? role.to_s
    end

  end

schema

  create_table "users", :force => true do |t|
t.string   "email",                  :default => "", :null => false
t.string   "encrypted_password",     :default => "", :null => false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",          :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at",                             :null => false
t.datetime "updated_at",                             :null => false
t.string   "name"
t.string   "title"
t.string   "pic_file_name"
  end

New Registration View

<%= simple_form_for(resource, :as => resource_name, :html => { :multipart => true }, :html => { :class => 'form-horizontal' }, :url => registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>
  <div class="row">
    <div class="span12 rounded-corners shadow inputs registration">
      <div class="row">
        <div class="span6">
          <h1>Join the Club</h1>
            <%= f.input :email, :required => true, :autofocus => true, :input_html => { :class => "span4" } %>
            <%= f.input :password, :required => true, :input_html => { :class => "span4" } %>
            <%= f.input :password_confirmation, :required => true, :input_html => { :class => "span4" } %>
            <%= f.input :name, :required => true, :input_html => { :class => "span4" } %>

            <%= f.input :title, :required => true, :input_html => { :class => "span4" } %>
            <%= f.input :pic_file_name, :as => :file, :label => "Upload Profile Picture", :required => true %>
        </div>
        <div class="span6 .hidden-phone ">
          <%= image_tag "signup.png", :class => "rounded-corners shadow signup_pic" %>
        </div>
      </div>




      <div class="row">
        <div class="signup_actions span3">
          <%= f.button :submit, "Sign up", :class => 'btn-warning btn-large' %></br >
          <%= render "links" %>
        </div>
      </div>

    <% end %>

  </div>

1
name is not a good name for a field. Is it easy for you to change that field to username?Anil
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) looks weird. It seems like your controller does not turn params into values properly.ohho
I could easily change the name of the field, I can see how that is a valuable change; however, I don't think doing so will fix the problem. Let me know if I'm wrong here.NelsonW

1 Answers

0
votes

Put name after attr_accessible: in user class.