我想我有一個路由問題與設計,我不明白。我正在嘗試執行this method,讓用戶使用Devise進行註冊以進行身份驗證。這個例子使用haml,我試圖把它翻譯回erb,希望取得一些成功。路由問題與設計可以確認 - 電子郵件只註冊,出現錯誤「找不到用戶id =確認」
的Rails 3.2.8在Mac OS X開發,使用PostgreSQL 使用設計和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
應用/配置/ routes.rb中:
devise_for :users, :controllers => {:confirmations => 'confirmations'}
devise_scope :user do
put "/confirm" => "confirmations#confirm"
end
resources :users
當我在users/sign_up頁面輸入我的電子郵件地址時,一切正常。我收到確認郵件後,點擊確認鏈接和我直接聯繫:
/users/confirmation?confirmation_token=[CONFIRMATION TOKEN HERE]
然後,當我填寫自己的密碼並提交,我進入這個網頁:
users/confirmation
具有以下錯誤:
ActiveRecord::RecordNotFound in UsersController#update Couldn't find User with id=confirmation
我很確定這是一個路由問題。有任何想法嗎?非常感謝!
似乎有一個重定向的問題,只是在黑暗中拍攝,但你可以嘗試改變你的simple_form_for的url到:':url => confirmation_path(資源)' – Noz
感謝您的建議,但沒有奏效。爲了清楚起見,你建議改變這一行,在views/devise/confirmations/show.html.erb: – Gruntled
<%= simple_form_for(resource,:as => resource_name,:url => confirmation_path(resource_name))do | f | %> to this:<%= simple_form_for(resource,:as => resource_name,:url => confirmation_path(resource))do | f | %>?在我看來,確認控制器永遠不會被調用。相反,rails是路由到用戶控制器中的show動作,這是錯誤的地方? – Gruntled