Jquery Rails验证非';不包括在第页中
我创建了一个示例应用程序,但编辑字段时遇到问题。每当我尝试更新我的字段时,它都会触发我的用户模型中的“validates:password\u confirmation”,即使我没有在页面中包含password\u confirmation字段 以下是我的一些代码: 这是我的“pages/services.html.erb”,我在这里显示数据Jquery Rails验证非';不包括在第页中,jquery,html,ruby-on-rails,Jquery,Html,Ruby On Rails,我创建了一个示例应用程序,但编辑字段时遇到问题。每当我尝试更新我的字段时,它都会触发我的用户模型中的“validates:password\u confirmation”,即使我没有在页面中包含password\u confirmation字段 以下是我的一些代码: 这是我的“pages/services.html.erb”,我在这里显示数据 <h1>Shows all users</h1> <% if current_user %> <table
<h1>Shows all users</h1>
<% if current_user %>
<table border="1">
<% @column_names.each do |column_name| %>
<% if column_name == "name" || column_name == "username" || column_name == "email"%>
<td>
<strong><%= column_name.capitalize %></strong>
</td>
<% end %>
<% end %>
<% @users.each do |user| %>
<tr>
<td><%= user.name%></td>
<td><%= user.username%></td>
<td><%= user.email%></td>
<td><%= link_to "edit", edit_user_path(user)%></td>
<td><%= link_to "delete", '#'%></td>
</tr>
<%end%>
</table>
<% end %>
显示所有用户
这是我的“users/edit.html.erb”。我有一个用户模型供参考
编辑
这是我的UsersController代码:
class UsersController < ApplicationController
def new
@user = User.new
@title = "User Sign Up"
end
def create
@user = User.new(params[:user])
if @user.save
sign_in_check @user
redirect_to root_path, :flash => { :success => "Welcome to the Bakeshop"}
else
@title = "User Sign Up"
render 'new'
end
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
redirect_to pages_services_path, :notice => "Update Successful"
else
render "edit"
end
end
end
class UsersController{:success=>“欢迎来到Bakeshop”}
其他的
@title=“用户注册”
呈现“新”
结束
结束
定义编辑
@user=user.find(参数[:id])
结束
def更新
@user=user.find(参数[:id])
如果@user.update_属性(参数[:user])
重定向到页面服务路径:注意=>“更新成功”
其他的
渲染“编辑”
结束
结束
结束
有人能解释这种现象并提出解决办法吗
编辑
添加了my user.rb文件代码:
class User < ActiveRecord::Base
require 'digest/md5'
attr_accessible :password_confirmation, :name, :email, :password, :username
before_save :encrypt_password
validates :name, :presence => true,
:length => { :maximum => 25}
validates :username, :presence => true,
:length => { :maximum => 25},
:uniqueness => {:case_sensitive => false}
validates :email, :presence => true,
:uniqueness => {:case_sensitive => false}
validates :password, :presence => true,
:confirmation => true,
:length => {:within => 6..40}
validates :password_confirmation, :presence => true
def self.authenticate(username, password)
user = User.find_by_username(username)
if user && user.password == Digest::MD5.hexdigest(password)
user
else
nil
end
end
def encrypt_password
self.password = Digest::MD5.hexdigest(password)
end
end
class用户true,
:length=>{:max=>25}
验证:username,:presence=>true,
:length=>{:max=>25},
:唯一性=>{:区分大小写=>false}
验证:email,:presence=>true,
:唯一性=>{:区分大小写=>false}
验证:password,:presence=>true,
:confirmation=>true,
:length=>{:within=>6..40}
验证:密码\u确认,:状态=>true
def自我验证(用户名、密码)
用户=用户。通过用户名(用户名)查找
如果user&&user.password==摘要::MD5.hexdigest(密码)
用户
其他的
无
结束
结束
def加密密码
self.password=Digest::MD5.hexdigest(密码)
结束
结束
我认为问题可能在这里:
validates :password, :presence => true,
:confirmation => true, #Confirm 1
:length => {:within => 6..40}
validates :password_confirmation, :presence => true #Confirm 2
在第一个validates
块中,我标记为confirm 1
的代码基本上与我标记的confirm 2
的代码执行相同的操作。第一个块是您所需要的全部,因为当有一个带有密码的字段时,此代码将自动确认它
第二位导致错误,因为它与密码字段没有明确关联,因此您要求Rails在创建或更新用户之前确认是否存在
:password\u confirmation
属性,这是不必要的。这有意义吗 Rails将根据类用户的验证器进行验证,即使您没有使用该类的所有字段
如果您不想在视图中使用或验证类用户的所有字段,则应使用另一个视图模型类,或仅删除您不想使用的验证(在本例中为密码确认)。是否可以添加
User.rb
文件?已添加User.rb代码。我明白了,我只是想验证我的密码确认字段是否存在。您是对的,通过删除验证方法,我的错误已经被修复。谢谢但我仍然不知道为什么要验证密码字段:(
validates :password, :presence => true,
:confirmation => true, #Confirm 1
:length => {:within => 6..40}
validates :password_confirmation, :presence => true #Confirm 2