Techioz Blog

Devise::RegistrationsController#create で NoMethodError を取得する

概要

Device gemを使用して新しいユーザーを追加しようとしています rake db: Migrate を実行し、Rails サーバーを再度実行しました

しかし、3000/posts/sign_upと入力すると

デフォルトのサインアップ画面が表示されますが、ユーザーを作成しようとすると、

デフォルトのサインアップ画面が表示されますが、ユーザーを作成しようとすると、

私のログには表示されます

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'

新しい.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

レーキルート

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

レール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

解決策

一番の問題はここにある

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

移行ファイルを確認してください。 encrypted_password は存在しますか?移行ファイルにこの t.string :encrypted_password, :null => false, :default => “” というコード行がありますか。また、モデルファイル(つまり、post.rb)では、パスワード属性も定義していません。