我有一個我創建的示例應用程序,並且在編輯字段時遇到問題。每當我嘗試更新字段時,即使我沒有在頁面中包含password_confirmation字段,也會在我的用戶模型中觸發我的「驗證:password_confirmation」。Rails驗證不包含在頁面中的字段
下面是我的一些代碼:
這是我的「頁/ services.html.erb」在這裏我顯示數據
<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 %>
這是我的「用戶/ edit.html.erb 」。我有一個用戶模型fyi。
編輯
<%= form_for @user do |f| %>
<%= render 'shared/error_message' %>
<p>
<%= f.text_field :name%><br/>
<%= f.label :name %><br/>
</p>
<p>
<%= f.text_field :username %><br/>
<%= f.label :username%><br/>
</p>
<p>
<%= f.email_field :email%><br/>
<%= f.label :email %><br/>
</p>
<p> <%= f.submit "Update" %></p>
<% end%>
,這裏是我的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
有人能解釋這一現象,並提出修復?
編輯
加了我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
你可以添加你的'user.rb'文件嗎? – dax
已添加user.rb代碼。 –