使用自定义update_password方法进行表单验证

时间:2014-02-28 20:15:26

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

我正在处理一个表单,用于更新当前项目中的用户密码。我正在使用Devise,但由于我们必须创建一个自定义的新用户表单,我还必须创建一个自定义表单来更新密码。我update_password中的UsersController方法指定了页面重定向,如果出现问题,我想更改它,以便此更新密码形式更接近匹配内联显示错误消息的创建表单。问题是,如果删除页面重定向,我会收到Template is missing错误。

完整的错误信息就是这个(它只是一行,但我将其分解,因此更容易阅读):

Missing template users/update_password, application/update_password with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :haml]}.
Searched in: * "/Users/kec6en/HydraApp/fluctus/app/views" * "/Users/kec6en/.rvm/gems/ruby-
2.0.0-p353@fluctus/gems/hydra-role-management-0.1.0/app/views" *
"/Users/kec6en/.rvm/gems/ruby-2.0.0-p353@fluctus/gems/devise-3.2.2/app/views" * 
"/Users/kec6en/.rvm/gems/ruby-2.0.0-p353@fluctus/gems/hydra-editor-0.2.2/app/views" *   
"/Users/kec6en/.rvm/gems/ruby-2.0.0-p353@fluctus/gems/blacklight-5.0.0.pre4/app/views" * 
"/Users/kec6en/.rvm/gems/ruby-2.0.0-p353@fluctus/gems/kaminari-0.15.1/app/views"

这是表格本身:

<div class="page-header">
  <h1>Change Password</h1>
</div>

<%= simple_form_for(@user, :url => { :action => "update_password" } ) do |f| %>
    <%= f.error_notification %>

    <div class="field">
      <%= f.label :password, "New Password" %><br />
      <%= f.password_field :password, :autocomplete => "off"  %>
    </div>
    <div class="field">
      <%= f.label :password_confirmation, "Confirm New Password" %><br />
      <%= f.password_field :password_confirmation %>
    </div>
    <div class="field">
      <%= f.label :current_password, "Current Password" %> <i>(we need your current password to confirm your changes)</i><br />
      <%= f.password_field :current_password %>
    </div>
    <br>
    <div class="action_container">
      <%= button_tag(type: 'submit', class: "btn doc-action-btn btn-success") do %>
          <i class="glyphicon glyphicon-check"></i> Submit
      <% end %>
      <%= link_to @user, {class: "btn doc-action-btn btn-cancel"} do %>
          <i class="glyphicon glyphicon-remove"></i> Cancel
      <% end %>
    </div>
<% end %>

这是我的路线文件的相关部分(以防万一):

resources :users do
    patch 'update_password', on: :collection
    get 'edit_password', on: :member
  end

这是用户控制器的相关部分(重定向被注释掉):

def edit_password
  @user = current_user
end

def update_password
  @user = User.find(current_user.id)
  if @user.update_with_password(user_params)
    sign_in @user, :bypass => true
    redirect_to root_path
    flash[:notice] = "Successfully changed password."
  else
    #redirect_to root_path
    flash[:alert] = "Current password was incorrect, new password was too short, or passwords did not match. Password has not been changed."
  end
end

基本上,我想更改else部分中的重定向和Flash通知部分以保持同一页面,但错误消息显示在表单顶部的列表中,或旁边相关表格输入。谢谢!

1 个答案:

答案 0 :(得分:0)

你会想要使用像

这样的东西
render :edit

使用您想要显示的任何视图更改:edit