开发者

Rails 3: how to pull change password out to be its own separate form from devise

I'm using devise for my user registrations and I'd like to have a separate edit path for user's to change/update their passwords. I've pulled the devise registrations views out and created a separate registrations controller like described in Railscast 236

I've tried creating a new action called change_password in the registrations controller but when I try to set the route with match '/change_password', to => 'registrations#change_password' I get an AbstractController::Actions Not Found

registrations controller

class RegistrationsController < Devise::RegistrationsController

def create
  super
  session[:omniauth] = nil unless @user.new_record?
 end

 def destroy
   resource.destroy 
   set_flash_message :notice, :destroyed
   sign_out_and_redirect(self.resource)
 end

 def change_password
   render_with_scope :edit
 end

private
  def build_resource(*args)
   super
   if session[:omniauth]
     @user.apply_omniauth(session[:omniauth])
     @user.valid?
   end
 end

routes.rb

  match 'auth/:provider/callback' => 'authentications#create'
  resources :authentications

  devise_for :users, :controllers => {:registrations => 'registrations'}




  resources :posts do
      member do
      get :likers
      end
       collection do
        get :search
      end
  end  

  resources :relationships, :only => [:create, :destroy]
  resources :appreciations, :only => [:create, :destroy]

   root :to => "pages#home"

  match '/contact', :to => 'pages#contact'
  match '/about',   :to => 'pages#about'
  match '/help',    :to => 'pages#help'
  match '/blog',    :to => 'pages#blog'


  resources :users do
     member do
     get :following, :followers, :likes
     end
     resour开发者_如何学Goces :collections
 end
end

views/registrations/change_password.html.erb

<h2>Edit <%= resource_name.to_s.humanize %></h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :validate => true,
                   :html => { :method => :put }, :html => {:multipart => true}) do |f| %>  
   <%= devise_error_messages! %> 
  <p><strong>To change password, otherwise leave blank.</strong></p>
  <p><%= f.label :current_password %> <i>(leave blank if you don't want to change it)</i><br />
  <%= f.password_field :current_password %></p>
  <p><%= f.label :password, "New password" %> <br />
  <%= f.password_field :password %></p>
  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>
  </div><br />
  <p><%= f.submit "Update" %></p>
<% end %>


Try to pass match .. in the block of devise_for helper:

devise_for :users, :controllers => {:registrations => 'registrations'} do
  match '/change_password', to => 'registrations#change_password'
end


I found this to be the easiest way to split the user edit form into edit & account

Originally I had

:name, 
:email, 
:avatar, 
:location, 
:website, 
:bio,  
:password (all in one form)

ROUTES (this provides route: account_user GET /users/:id/account(.:format) users#account)

resources :users do
  member do
    get :following, :followers, :account
  end  
end

USERS_CONTROLLER.RB

before_filter :signed_in_user, only: [:index, :edit, :update, :destroy, :following, :followers, :account]
before_filter :correct_user,   only: [:edit, :update, :account]


def edit
  @user = User.find(params[:id])
end

def account
  @title = "Account"
  @user = User.find(params[:id])
end

def update
  if @user.update_attributes(params[:user])
    flash[:success] = "Profile updated"
    sign_in @user
    redirect_to @user
  elsif @title = "Account"
    render 'account'
  else
    render 'edit'
  end
end

(split form views)

EDIT.HTML.ERB

<%= form_for @user, :html => { :multipart => true } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :avatar %><br />
  <%= f.file_field :avatar %>

  <%= f.label :location %>
  <%= f.text_field :location %>

  <%= f.label :website %>
  <%= f.text_field :website %>

  <%= f.label :bio %>
  <%= f.text_area :bio, placeholder: "About yourself in 160 characters or less..." %>

  <%= f.submit "Update Profile", class: "btn btn-medium btn-primary" %>
<% end %>

ACCOUNT.HTML.ERB

<%= form_for @user, :html => { :multipart => true } do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirm Password" %>
  <%= f.password_field :password_confirmation %>

  <%= f.submit "Update Account", class: "btn btn-medium btn-primary" %>
<% end %>

SIDE NAV BAR INSIDE OF EDIT AND ACCOUNT VIEWS, SO THE USER HAS ACCESS TO EDIT AND ACCOUNT FORM

<ol class="nav nav-tabs nav-stacked">
  <% @user ||= current_user %>
    <li>
      <a href="<%= edit_user_path(@user) %>">
      Profile
      <i class="icon-chevron-right"></i>
      </a>
    </li>
    <li>
      <a href="<%= account_user_path(@user) %>">
      Account
      <i class="icon-chevron-right"></i>
      </a>
    </li>
</ol>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜