Rails 3:如何将更改密码从设计中拉出来成为自己独立的形式

时间:2011-05-15 09:17:21

标签: ruby-on-rails-3 forms devise

我正在使用设备进行用户注册,我希望有一个单独的编辑路径供用户更改/更新其密码。我已经删除了设计注册视图,并创建了一个单独的注册控制器,如Railscast 236

中所述

我尝试在注册控制器中创建一个名为change_password的新操作,但是当我尝试设置匹配'/ change_password'的路由时,= => 'registrations#change_password'我得到一个AbstractController :: Actions Not Found

注册控制器

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
     resources :collections
 end
end

视图/注册/ 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 %>

2 个答案:

答案 0 :(得分:1)

尝试在devise_for帮助程序块中传递match ..

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

答案 1 :(得分:0)

我发现这是将用户编辑表单拆分为编辑和放大的最简单方法。帐户

最初我有

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

ROUTES(提供路线:   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

(拆分表单视图)

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 %>

编辑和账户视图内的侧边栏,因此用户可以访问编辑和帐户表格

<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>