0
votes

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"=>"[email protected]", "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
1
This common question and it have answers like this: stackoverflow.com/questions/15159355/… - Sergey Moiseev
nope still not working after trying these methods , still get same error - Neil
@Neil Did you perform rails generate devise:install? - Kirti Thorat
@Neil Also make sure that you restart the application. After performing all devise setup steps for the changes to take effect. - Kirti Thorat
yes ran the install command and restarted but its ok i will rollback and migrate again , thanks - Neil

1 Answers

1
votes

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.