问题描述
尝试添加新用户,使用devise gem
运行rake db:迁移并再次运行rails服务器
但是当我键入3000 /帖子/ sign_up
它给了我默认的注册屏幕,但是当尝试创建一个用户我得到
在Devise中的NoMethodError :: RegistrationsController#创建
未定义的方法`encrypted_password ='for#
我的日志显示
在2014-03-18 20:38:22启动POST/ posts为127-03.0 20:38:22 +0000
Devise :: RegistrationsController#创建为HTML
参数:{utf8=>✓,authenticity_token="2uO2ttqAfK1a2C855cZOpDTsS2Duc7ZzVJxAQ5ObL8M =,post=> {email=>[email protected],密码=>FILTERED],password_confirmation=>[FILTERED]},commit=>注册}
79ms内完成500内部服务器错误
NoMethodError(未定义的方法`encrypted_password ='for#< Post:0x007fde3e09e538>):
activemodel(4.0。 4)lib / active_model / attribute_methods.rb:439:在`method_missing'
activerecord(4.0.4)lib / active_record / attribute_methods.rb:167:在`method_missing'
devise(3.2.2) lib / devise / models / database_authenticatable.rb:42:在`password ='
activerecord(4.0.4)lib / active_record / attribute_assignment.rb:42:在`public_send'
activerecord(4.0.4 )lib / active_record / attribute_assignment.rb:42:在`_assign_attribute'
activerecord(4.0.4)lib / active_record / attribute_assignment.rb:29:在`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:在`assign_attributes'
new.html.erb
< h2& ;注册< / h2>
<%= form_for(resource,:as => resource_name,:url => registration_path(resource_name))do | f | %GT;
<%= devise_error_messages! %GT;
< 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注册%>< / div>
<%end%>
<%= renderdevise / shared / links%>
post.rb
class Post< ActiveRecord :: Base
#包含默认设计模块。其他可用的是:
#:confirmable,:lockedable,:timeoutable和:omniauthable
devise:database_authenticatable,:可注册,
:可恢复,可记忆,可追溯,可验证
结束
rake路线
前缀动词URI模式控制器#动作
new_post_session GET /posts/sign_in(.:format)devise / sessions#new
post_session POST /posts/sign_in(.:format)devise / session#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 / post / password / new(。:format)devise / passwords#new
edit_post_password GET /posts/password/edit(.:format)devise / passwords#edit
PATCH /posts/password(.:format )devise / password#update
PUT /posts/password(.:format)devise / passwords#update
cancel_post_registration GET /posts/cancel(.:format)devise / registrations#cancel
post_registration POST /posts(.:format)devise /注册#创建
new_post_registration GET /posts/sign_up(.:format)设计/注册#new
edit_post_registration GET /posts/edit(.:format)设计/注册#edit
PATCH / posts (。:格式)设计/注册#update
PUT /posts(.:format)设计/注册#update
DELETE /posts(.:format)devise / registrations#destroy
posts GET /posts(.:format)帖子#index
POST /posts(.:format)帖子#创建
new_post GET /posts/new(.:format)帖子#new
edit_post GET /posts/:id/edit(.:format)posts#edit
post GET /posts/:id(.:format)posts#show
PATCH /posts/:id(.:format )#update
PUT /posts/:id(.:format)帖子#update
DELETE /posts/:id(.:format)帖子#destroy
关于GET / about(。 :格式)page#about
root GET / pages#welcome
rails g
Neils-MacBook-Pro:prospects neilpatel $ rails g devise:views
/ pre>
invoke Devise :: Generators :: SharedViewsGenerator
创建app / views / devise / shared
创建app / views / devise / shared / _links.erb
调用form_for
创建应用程序/视图/ devise / confirmations
创建应用程序/ views / devise / confirmations / new.html.erb
创建app / views / devise / passwords
创建app / views / devise / passwords / edit.html.erb
创建app / views / devise / passwords / new.html.erb
创建应用程序/ views / devise / registrations
创建app / views / devise / registrations / edit.html.erb
创建app / views / devise / registrations / new.html.erb
创建应用/视图/ devise / sessions
创建app / views / devise / sessions / new.html.erb
创建app / views / devise / unlocks
创建app / views / devise / unlocks / new.html .erb
调用erb
创建app / views / devise / mailer
创建app / views / devise / mailer / confirmation_instructions.html.erb
创建app / views / devise / mailer /reset_password_instructions.html.erb
创建app / views / devise / mailer / unlock_instructions.html.erb
Neils-MacBook-Pro:prospects neilpatel $ rails generate devise Post
invoke active_record
创建db / migrate / 20140 318202353_add_devise_to_posts
insert app / models / post.rb
route devise_for:posts
Neils-MacBook-Pro:prospects neilpatel $ rake db:migrate
解决方案主要问题在这里
NoMethodError(未定义的方法`encrypted_password ='for#< Post:0x007fde3e09e538>):
查看你的迁移文件。 encrypted_password是否存在?你有这个
t.string:encrypted_password,:null => false,:default =>
您的迁移文件中的代码行。而在你的模型文件(即post.rb)中,你也没有定义password属性。trying to add new users , using the devise gemran 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#createundefined 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
解决方案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.这篇关于在Devise :: RegistrationsController#create中获取NoMethodError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!