2013-08-28 35 views
0

我有一個我創建的示例應用程序,並且在編輯字段時遇到問題。每當我嘗試更新字段時,即使我沒有在頁面中包含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 
+0

你可以添加你的'user.rb'文件嗎? – dax

+0

已添加user.rb代碼。 –

回答

0

我認爲這個問題可能會在這裏:

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_confirmation屬性的存在,這是不必要的。那有意義嗎?

+0

我看到了,我只是想我想驗證我的password_confirmation字段中的存在。你是對的,通過刪除驗證方法,我的錯誤已得到修復。謝謝。但我仍然不知道爲什麼它驗證password_field :( –

+0

編輯解釋更好 – dax

+0

是的,我想我得到了它的要點,謝謝。 –

0

即使您沒有使用該類的所有字段,Rails也會根據類User的驗證器進行驗證。

如果您不想在視圖中使用或驗證類User的所有字段,則應該使用另一個視圖模型類,或者直接刪除不想使用的驗證(在本例中爲password_confirmation )。