1

Im getting an error trying to add the value name to devise, I think all did all things to make it work, but it doesnt work.

NoMethodError in Devise/registrations#new

undefined method `name' for #<User:0x00000002e4e038>

Here is my user model:

class User < ActiveRecord::Base

  include Gravtastic
  gravtastic :size => 165, :filetype => :png, :rating => 'R'

  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  validates :email, :username, :presence => true, :uniqueness => true

  # Setup accessible (or protected) attributes for your model
  attr_accessible :name, :username, :email, :password, :password_confirmation, :remember_me

  has_many :topics, :dependent => :destroy
  has_many :posts, :dependent => :destroy

  def admin?
    true if self.username == 'admin'
  end

end

Here is my registration view:

<h2>Sign up</h2>

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

  <p><%= f.label :name %><br />
  <%= f.text_field :name %></p>

  <p><%= f.label :email %><br />
  <%= f.email_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

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

<%= render :partial => "devise/shared/links" %>

And here is my devise registration view:

div class="module" style="padding:15px 25px 0px 25px;">

  <div style="float:right; width:100%; padding-left:30px; border-left:1px solid #e2e2e2;">
    <%= form_for("user", :as => resource_name, :url => registration_path("user")) do |f| %>
      <%= devise_error_messages! %>
      <p>
     Nome: <br />
        <%= f.text_field :name, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
     Usuario: <br />
        <%= f.text_field :username, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Email: (apenas emails @usp.br sao aceitos) <br />
        <%= f.text_field :email, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Senha:<br />
        <%= f.password_field :password, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p>
        Confirmar senha:<br />
        <%= f.password_field :password_confirmation, :style => "font-size:2.0em", :autocomplete => "off" %><br />
      </p>
      <p><%= f.submit "Criar" %></p>
    <% end %>
  </div>
  <div class="clear"></div>
</div>

I already tryied rake db:migrate but it still doesnt solve my problem. Sorry, Im still a noob on rails.

EDIT: Here is my migration file:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable
      t.confirmable

      # t.encryptable
      # t.confirmable
      # t.lockable :lock_strategy => :failed_attempts, :unlock_strategy => :both
      # t.token_authenticatable


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end

  def self.down
    drop_table :users
  end
end

EDIT 2: Here is my schema.rb

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20131121000236) do

  create_table "categories", :force => true do |t|
    t.string   "title"
    t.boolean  "state",      :default => true
    t.integer  "position",   :default => 0
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "forums", :force => true do |t|
    t.string   "title"
    t.text     "description"
    t.boolean  "state",        :default => true
    t.integer  "topics_count", :default => 0
    t.integer  "posts_count",  :default => 0
    t.integer  "position",     :default => 0
    t.integer  "category_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", :force => true do |t|
    t.text     "body"
    t.integer  "forum_id"
    t.integer  "topic_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "topics", :force => true do |t|
    t.string   "title"
    t.integer  "hits",        :default => 0
    t.boolean  "sticky",      :default => false
    t.boolean  "locked",      :default => false
    t.integer  "posts_count"
    t.integer  "forum_id"
    t.integer  "user_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "email",                                 :default => "", :null => false
    t.string   "encrypted_password",     :limit => 128, :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.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "topics_count",                          :default => 0
    t.integer  "posts_count",                           :default => 0
    t.string   "username"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end
Maia
  • 51
  • 6

1 Answers1

5

At least from that migration file, it seems like you are missing name column for users table.

Add the name column for users table.

rails g migration AddNameToUsers

class AddNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :name, :string
  end
end

Run rake migrate.

Restart server just to be safe.


FINAL Edit. NEver again!!

In the devise migration file, uncomment

t.string   :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string   :unconfirmed_email # Only if using reconfirmable

rake db:drop db:create db:migrate

restart server

Jason Kim
  • 18,102
  • 13
  • 66
  • 105
  • 1
    Still didn't work, it gives me the same error. I don't know what to do.. but thanks anyways @dmtri.com – Maia Nov 21 '13 at 00:06
  • 1
    Try doing `rake migrate` and restarting the server – Jason Kim Nov 21 '13 at 00:06
  • shouldn't it be rake db:migrate? I already did that and still doesnt work. Do you think my schema.rb will help? @dmtri.com – Maia Nov 21 '13 at 00:09
  • Is there a reason (s)he should restart the server? – Zippie Nov 21 '13 at 00:09
  • 1
    @Maia, sure, post your schema.rb as well – Jason Kim Nov 21 '13 at 00:12
  • 1
    @Zippie, just good practice in general. It's a habit that stuck to me when I was doing Rails in 1.x days. Also depending on how Rails, database are set up, explicit restart is required. – Jason Kim Nov 21 '13 at 00:15
  • 1
    @Maia, it seems like you are using both `username` and `name`. Is this intentional? I still don't see `name` column in `users` table. – Jason Kim Nov 21 '13 at 00:17
  • Yeah, i know it's a good practice overall, cause stuff can get weird sometimes, but unless your introducing some new initializer files, i don't know if its necessary to restart your server. – Zippie Nov 21 '13 at 00:17
  • 2
    @Zippie I don't disagree Zippie. Just the nature of working on SO too I guess. A lot of mysterious problems people ask somehow gets magically solved with server restart or bundle install and stuff. So I just recommend it. – Jason Kim Nov 21 '13 at 00:21
  • @dmtri.com Yes, its for a forum, username is the user and name is the person name. I dont know why name column isnt in user table, I did what you said first, but it still doesnt work. – Maia Nov 21 '13 at 00:21
  • 1
    Agreed. Was just asking if i'm missing something specific to this question :) – Zippie Nov 21 '13 at 00:22
  • 1
    @Maia, can you show me the new migration file you just created to add the name column to users table? – Jason Kim Nov 21 '13 at 00:23
  • @dmtri.com #20131121000236_add_name_to_users.rb `class AddNameToUsers < ActiveRecord::Migration def change add_column :users, :name, :string end end` – Maia Nov 21 '13 at 00:25
  • 1
    Yes, can you post the content of it? – Jason Kim Nov 21 '13 at 00:26
  • 1
    Is it ok to wipe out the data on your database? – Jason Kim Nov 21 '13 at 00:27
  • No problem, `rake db:drop db:create db:migrate` ? – Maia Nov 21 '13 at 00:28
  • @dmtri.com it still doesnt work, same error, can I send you all the project? – Maia Nov 21 '13 at 00:31
  • @dmtri.com Here is it if you can take a look, I know Im asking too much but I need to fix that for tomorrow, sorry: http://www.mediafire.com/?99vs857x269sqtt – Maia Nov 21 '13 at 00:34
  • 1
    @Maia, did you get any error running `rake migrate`? Because I get error. If `rake migrate` fails, the database will not get updated. As far as I can see, I think root of the problem might be still using `rails 3.0.9`. This is old. – Jason Kim Nov 21 '13 at 00:43
  • @dmtri.com You have a good point, I will try on rails 3.2.14, updating here – Maia Nov 21 '13 at 00:50
  • @dmtri.com when I updated to rails 3.2.14 I get an error while trying to run the server: `actionpack-3.2.14/lib/action_view/railtie.rb:42:in block (3 levels) in : undefined method debug_rjs=' for ActionView::Base:Class (NoMethodError)` – Maia Nov 21 '13 at 00:52
  • 1
    Did you do `bundle update` and `bundle install`? Also restarting server? – Jason Kim Nov 21 '13 at 00:56
  • @dmtri.com yes, I did – Maia Nov 21 '13 at 00:58
  • 1
    http://stackoverflow.com/questions/7718541/error-nomethoderror-undefined-method-debug-rjs-for-actionviewbaseclass According to that we have to disable debug_rjs in config/development.rb. – Jason Kim Nov 21 '13 at 01:00
  • @dmtri.com I did that, now I get another error, damm, `undefined method use_salt_as_remember_token= for Devise:Module (NoMethodError)` – Maia Nov 21 '13 at 01:01
  • 1
    Comment out `# config.use_salt_as_remember_token = true` in devise.rb – Jason Kim Nov 21 '13 at 01:02
  • Nice, now its working, but `rake migrate` gives me an error: `rake migrate rake aborted! Don't know how to build task 'migrate' /home/luca/.rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in eval' /home/luca/.rvm/gems/ruby-1.9.3-p448/bin/ruby_executable_hooks:15:in
    ' (See full trace by running task with --trace)`
    – Maia Nov 21 '13 at 01:08
  • 1
    Replace the migration file `20110808220236_devise_create_users.rb`, with [this](https://gist.github.com/serv/7574299). Then `rake db:drop db:create db:migrate`. Check if name is in the users table – Jason Kim Nov 21 '13 at 01:12
  • Im almost dancing here man. YOU ARE MY HERO, REALLY, I NEED TO PAY YOU A BEER! IT'S WORKING!!!!!!!!!! – Maia Nov 21 '13 at 01:16
  • @dmtri.com I'm sorry to bother you again, but I dont have anyone to recover. I was so happy that I didint test creating an user. It gives me an error and I couldnt fix it searching on the internet: `NameError in Devise::RegistrationsController#create undefined local variable or method confirmed_at'` – Maia Nov 21 '13 at 01:28
  • THANKS! I wont forget this :) – Maia Nov 21 '13 at 01:40