我想我有一个我无法弄清楚的 Devise 路由问题。我正在尝试实现这种方法,让用户使用 Devise 进行身份验证。该示例使用了haml,我试图将其翻译回erb,希望能取得一些成功。
在 Mac OS X 上开发 Rails 3.2.8,使用 PostgresQL 使用 Devise 和 simple_form
应用程序/视图/设计/注册/new.html.erb:
<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
<%# simple_form_for([:backend, @user]) do |f| %>
<%= f.error_notification %>
<div class="inputs">
<%= f.input :email, :required => true, :autofocus => true, :label => "Email" %>
</div>
<div class="actions">
<%= f.button :submit, "Add User" %>
</div>
<% end %>
应用程序/视图/设计/确认/show.html.erb:
<%= simple_form_for(resource, :as => resource_name, :url => confirmation_path(resource_name)) do |f| %>
<div class="inputs">
<%= f.input :password, :required => true, :label => "Password" %>
<%= f.input :password_confirmation, :required => true, :label => "Confirm Password" %>
<%= f.hidden_field :confirmation_token %>
</div>
<div class="actions">
<%= f.button :submit, "Confirm" %>
</div>
<% end %>
应用程序/modesl/user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
:timeoutable
# Setup accessible (or protected) attributes for your model
attr_accessible :email,
:password,
:password_confirmation,
:remember_me,
:username
def password_required?
super if confirmed?
end
def password_match?
self.errors[:password] << "can't be blank" if password.blank?
self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
password == password_confirmation && !password.blank?
end
end
应用程序/控制器/confirmations_controller.rb:
class ConfirmationsController < Devise::ConfirmationsController
def show
self.resource = resource_class.find_by_confirmation_token(params[:confirmation_token])
super if resource.confirmed?
end
def confirm
self.resource = resource_class.find_by_confirmation_token(params[resource_name][:confirmation_token])
if resource.update_attributes(params[resource_name].except(:confirmation_token)) && resource.password_match?
self.resource = resource_class.confirm_by_token(params[resource_name][:confirmation_token])
set_flash_message :notice, :confirmed
sign_in_and_redirect(resource_name, resource)
else
render :action => "show"
end
end
end
应用程序/配置/路由.rb:
devise_for :users, :controllers => {:confirmations => 'confirmations'}
devise_scope :user do
put "/confirm" => "confirmations#confirm"
end
resources :users
当我在用户/注册页面输入我的电子邮件地址时,一切正常。我收到确认电子邮件,单击确认链接并被定向到:
/users/confirmation?confirmation_token=[此处确认令牌]
然后,当我填写密码并提交时,我被定向到此页面:
用户/确认
出现以下错误:
UsersController#update 中的 ActiveRecord::RecordNotFound 找不到 id=confirmation 的用户
我很确定这是一个路由问题。有任何想法吗?非常感谢!