你可以重寫Devise的默認行爲來使其工作。通過將您的show
行動統一到設計RegistrationsController
,然後宣佈的行動路線開始:
# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def show
end
end
# config/routes.rb
devise_for :users, :controllers => { :registrations => "registrations" }
devise_scope :user do
get "users/show"=> "users/registrations#show", :as => "show_registration"
end
然後,在你RegistrationsController#show
行動,創建resource
一個實例來傳遞給視圖:
# app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
def show
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
end
最後,向您的show.html.erb
視圖添加一個表單,該表單將提交至RegistrationsController#update
操作。您可以直接從default Devise registration/edit.html.erb
template複製此:
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
<div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
<% end %>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
end
瞧!您的自定義show
操作將包含一個表單,該表單將當前註冊資源提交給默認Devise RegistrationsController#update
操作。
謝謝,現在就試試這個。 – miler350
當然,請告知它是否有效...我自己並沒有實際執行它。 – zeantsoi
工作就像一個魅力。 – miler350