1

我正在使用这个 gem:https ://github.com/bootstrap-ruby/rails-bootstrap-forms 。如果未满足验证,gem 应该以红色突出显示输入框。我有几个问题。首先,内联验证错误不起作用。其次,更重要的是,我的模型验证错误根本不起作用。

如何更改验证以确保它们有效?我需要做一些特别的事情来让 gem 在前端工作吗?还是最好使用另一种方法来解决前端验证错误?

太感谢了!

编辑.html.erb

<!-- Change Password Modal -->

<div id="changepasswordmodal" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">×</button>
                  <h3 class="">Change Your Password</h3>
            </div>
            <div class="modal-body">
              <%= bootstrap_form_for @user, url: change_password_path, label_errors: true do |p| %>
              <%= p.password_field :current_password, label: "Old Password", placeholder: "Old Password", class: "input-lg required" %>
              <%= p.password_field :password, label: "New Password", placeholder: "New Password", class: "input-lg required" %>
              <%= p.password_field :password_confirmation, label: "Confirm New Password", placeholder: "Confirm New Password", class: "input-lg required"%>

              <%= p.submit "Change Password", :class=> "btn btn-primary" %>
              <% end %>
            </div>
        </div>
    </div>
</div>

users_controller.rb

def change_password
    @user = User.find(@current_user)
    current_password = params[:user][:current_password]
    user = User.authenticate(@user.email, current_password)
    if @user && user
      User.update(@user, change_password_params)
      @user.save
      flash[:success] = "Password successfully changed!"
      redirect_to edit_user_path(@current_user)
    else
      flash[:danger] = "Your old password was incorrect. Please try again."
      redirect_to edit_user_path(@current_user)
    end

  end

  private

  def change_password_params
    params.require(:user).permit(:password, :password_confirmation)
  end

用户.rb

  validates :password, :presence =>true, :confirmation =>true
  validates_confirmation_of :password
4

0 回答 0