Rails验证不适用于' rails-bootstrap-forms / bootstrap_form_for'

时间:2015-09-30 22:33:06

标签: ruby-on-rails twitter-bootstrap validation twitter-bootstrap-form

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

如何更改验证以确保它们有效?为了让宝石在前端工作,我需要做些什么特别的事情吗?或者最好使用另一种方法进行前端验证错误?

非常感谢你!

  

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

user.rb

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

0 个答案:

没有答案