简体   繁体   中英

Editing Devise From Users#show view

I have a Rails 3.2 application that uses Devise for authentication. Currently, the new_user_registration works fine and the edit_user_registration paths both work as designed. However, I have designed a tabbed user profile where the user will have access to various forms (edit registration, site settings etc.)

The Issue

Although the users#show page contains a partial that pulls in the form for editing registration, it doesn't actually allow the user to edit. I would just recreate an edit action in the users controller, but I want to keep some of Devise's built in functionality (such as lost password, etc. not sure if this is effected).

Is there any way I can I have the user edit registrations#edit action on the users#show view?

You'll can override Devise's default behavior to get this to work. Start by bringing your show action into the Devise RegistrationsController , then declaring a route to the action:

# 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

Then, in your RegistrationsController#show action, create an instance of resource to pass to the view:

# 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

Finally, add a form to your show.html.erb view that will submit to the RegistrationsController#update action. You can copy this directly from the 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

Voilà! Your custom show action will contain a form that submits the current registrations resource to the default Devise RegistrationsController#update action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM