1

trying to add new users , using the devise gem ran rake db:migrate and ran the rails server again

but when i type in 3000/posts/sign_up

it gives me the default sign up screen but when try to create a user i get

NoMethodError in Devise::RegistrationsController#create undefined method `encrypted_password=' for #

my logs show

Started POST "/posts" for 127.0.0.1 at 2014-03-18 20:38:22 +0000
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"2uO2ttqAfK1a2C855cZOpDTsS2Duc7ZzVJxAQ5ObL8M=", "post"=>{"email"=>"neil@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 500 Internal Server Error in 79ms

NoMethodError (undefined method `encrypted_password=' for #<Post:0x007fde3e09e538>):
  activemodel (4.0.4) lib/active_model/attribute_methods.rb:439:in `method_missing'
  activerecord (4.0.4) lib/active_record/attribute_methods.rb:167:in `method_missing'
  devise (3.2.2) lib/devise/models/database_authenticatable.rb:42:in `password='
  activerecord (4.0.4) lib/active_record/attribute_assignment.rb:42:in `public_send'
  activerecord (4.0.4) lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
  activerecord (4.0.4) lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
  activerecord (4.0.4) lib/active_record/attribute_assignment.rb:23:in `each'
  activerecord (4.0.4) lib/active_record/attribute_assignment.rb:23:in `assign_attributes'

new.html.erb

<h2>Sign up</h2>

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

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

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

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

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

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

post.rb

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

rake routes

Prefix Verb   URI Pattern                    Controller#Action
        new_post_session GET    /posts/sign_in(.:format)       devise/sessions#new
            post_session POST   /posts/sign_in(.:format)       devise/sessions#create
    destroy_post_session DELETE /posts/sign_out(.:format)      devise/sessions#destroy
           post_password POST   /posts/password(.:format)      devise/passwords#create
       new_post_password GET    /posts/password/new(.:format)  devise/passwords#new
      edit_post_password GET    /posts/password/edit(.:format) devise/passwords#edit
                         PATCH  /posts/password(.:format)      devise/passwords#update
                         PUT    /posts/password(.:format)      devise/passwords#update
cancel_post_registration GET    /posts/cancel(.:format)        devise/registrations#cancel
       post_registration POST   /posts(.:format)               devise/registrations#create
   new_post_registration GET    /posts/sign_up(.:format)       devise/registrations#new
  edit_post_registration GET    /posts/edit(.:format)          devise/registrations#edit
                         PATCH  /posts(.:format)               devise/registrations#update
                         PUT    /posts(.:format)               devise/registrations#update
                         DELETE /posts(.:format)               devise/registrations#destroy
                   posts GET    /posts(.:format)               posts#index
                         POST   /posts(.:format)               posts#create
                new_post GET    /posts/new(.:format)           posts#new
               edit_post GET    /posts/:id/edit(.:format)      posts#edit
                    post GET    /posts/:id(.:format)           posts#show
                         PATCH  /posts/:id(.:format)           posts#update
                         PUT    /posts/:id(.:format)           posts#update
                         DELETE /posts/:id(.:format)           posts#destroy
                   about GET    /about(.:format)               pages#about
                    root GET    /                              pages#welcome

rails g

Neils-MacBook-Pro:prospects neilpatel$ rails g devise:views
      invoke  Devise::Generators::SharedViewsGenerator
      create    app/views/devise/shared
      create    app/views/devise/shared/_links.erb
      invoke  form_for
      create    app/views/devise/confirmations
      create    app/views/devise/confirmations/new.html.erb
      create    app/views/devise/passwords
      create    app/views/devise/passwords/edit.html.erb
      create    app/views/devise/passwords/new.html.erb
      create    app/views/devise/registrations
      create    app/views/devise/registrations/edit.html.erb
      create    app/views/devise/registrations/new.html.erb
      create    app/views/devise/sessions
      create    app/views/devise/sessions/new.html.erb
      create    app/views/devise/unlocks
      create    app/views/devise/unlocks/new.html.erb
      invoke  erb
      create    app/views/devise/mailer
      create    app/views/devise/mailer/confirmation_instructions.html.erb
      create    app/views/devise/mailer/reset_password_instructions.html.erb
      create    app/views/devise/mailer/unlock_instructions.html.erb
Neils-MacBook-Pro:prospects neilpatel$ rails generate devise Post
      invoke  active_record
      create    db/migrate/20140318202353_add_devise_to_posts
      insert    app/models/post.rb
       route  devise_for :posts
Neils-MacBook-Pro:prospects neilpatel$ rake db:migrate
Neil
  • 141
  • 2
  • 12

1 Answers1

1

The main problem is here

NoMethodError (undefined method `encrypted_password=' for #<Post:0x007fde3e09e538>):

Check out you migration file. Is encrypted_password present or not? Do you have this t.string :encrypted_password, :null => false, :default => "" line of code in your migration file. And in your model file (i.e post.rb) you haven't define password attribute too.

Amrit Dhungana
  • 4,371
  • 5
  • 31
  • 36
  • i have this t.string :encrypted_password, :null => false, :default => "" in my migration file but my post.rb is this devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable do i add :encrypted_password – Neil Mar 18 '14 at 21:16
  • Have you add "attr_accessible :email, :password, :password_confirmation, :remember_me" in your post.rb – Amrit Dhungana Mar 18 '14 at 21:21
  • no because thought i didnt think we use that command on rails 4? – Neil Mar 18 '14 at 21:26
  • Sorry, You are right you don't need thtat line in rails4. If show rollback the db and migrate it again. – Amrit Dhungana Mar 18 '14 at 21:36
  • Check this link http://andreapavoni.com/blog/2013/8/a-rails-4-tutorial-application-for-beginners . It will help you. – Amrit Dhungana Mar 18 '14 at 21:40
  • I tried adding the migration and the attributes in the userscontroller and it did not work http://stackoverflow.com/posts/30269771/edit – codigomonstruo May 15 '15 at 23:44
  • def change add_column :users, :provider, :string add_column :users, :uid, :string add_colum :users, :encrypted_password, :string end end – codigomonstruo May 15 '15 at 23:49