【问题标题】:Form validation with custom update_password method使用自定义 update_password 方法进行表单验证
【发布时间】:2014-02-28 20:15:25
【问题描述】:

我正在制作用于更新当前项目中用户密码的表单。我正在使用Devise,但是因为我们必须创建一个自定义的新用户表单,所以我还必须创建一个自定义表单来更新密码。我的UsersController 中的update_password 方法在出现问题时指定页面重定向,我想更改它,以便此更新密码表单更接近内联显示错误消息的创建表单。问题是,如果页面重定向被删除,我会收到 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 通知部分以保持在同一页面上,但错误消息显示在表单顶部的列表中,或相关的旁边表单输入。谢谢!

【问题讨论】:

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


    【解决方案1】:

    你会想要使用类似的东西

    render :edit
    

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

    【讨论】:

    • 就是这样,我不想显示不同的视图,我希望能够使用简单表单中的内置错误通知。我根本不希望页面重定向。但是如果我删除重定向语句,事情就会中断。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    相关资源
    最近更新 更多