0

我的注册页面已登录。电子邮件、密码、密码确认和公司字段。所有这些都应该是强制性的,但是当我单击注册时,它不会检查除密码和密码确认之外的任何字段是否为空白!!我不明白我的 app/models/user.rb 文件有什么区别,我验证了所有字段的存在。请帮我

这是代码(必填行)

用户.rb

class User < ActiveRecord::Base
  has_many :excel_files         # One user may have many excel files
  has_one  :user_access_validity# One user may have one license period 

  # Virtual attribute for the unencrypted password
  attr_accessor :password

  attr_accessible :login
  attr_accessible :email
  attr_accessible :password
  attr_accessible :password_confirmation
  attr_accessible :company


  #changes of 'validates' in accordance with rails 3:

  validates :login,  :presence => true, 
                        :length => { :within => 3..40}, 
                        :uniqueness => { :case_sensitive => false },
                        :format => { :with =>  /^([a-z_0-9\.]+)$/i },
                        :on => :create, 
                        :if => :is_login_entered?
  validates :email, :presence => true, 
                        :length => { :within => 7..100}, 
                        :uniqueness => { :case_sensitive => false },
                        :format => {:with => /^([a-z]+((\.?)|(_?))[a-z0-9]+@(mindtree.com|rvce.edu.in))$/i},
                        :on => :create,
                        :if => :is_email_entered? 
  validates :company, :presence => true,
                        :format => { :with =>/(mindtree|RVCE)/i},
                        :format => { :with => /^([a-z]+)$/i },
                        :on => :create, 
                        :if => :is_company_entered? 

   validates :password, :presence => true,
                       :length => { :within => 4..40 },
                       :confirmation => true,
                       :format => { :with => /^([a-z0-9@!#\$]+)$/i },
                       :on => :create,
                       :if => :password_required?

  validates :password_confirmation, :presence => { :if => :password_required? }



  before_save :encrypt_password


  #
  # is_email_entered? : checks whether the email field is entered or not
  # @params           : none
  # @return           : true  - if the email is entered
  #                     false - if the email is not entered
  # 
  def is_email_entered?
    !self.email.blank?
  end

  #
  # is_company_entered? : checks whether the company field is entered or not
  # @params             : none
  # @return             : true  - if the company is entered
  #                       false - if the company is not entered
  #
  def is_company_entered?
    !self.company.blank?
  end

  #
  # is_login_entered? : checks whether the login field is entered or not
  # @params           : none
  # @return           : true  - if the login is entered
  #                     false - if the login is not entered
  #
  def is_login_entered?
    !self.login.blank?
    puts "login"
  end



  protected



    # 
    # password_required? : Checks whether either of the crypted_password and password field is blank
    # @params            : none
    # @return            : true  - if either of the crypted_password and password field is blank
    #                      false - if either of the crypted_password and password field is not blank
    # 
    def password_required?
      crypted_password.blank? || !password.blank?
      puts "in pr?"
    end


end

控制器.rb

def signup

        if logged_in?
            flash[:notice] = "<span class='error'>You have already registered with us!</span>".html_safe
            redirect_to :action => 'upload_file'
        else 
            @user = User.new(params[:user])
            return unless request.post?
            @user.save!
            self.current_user = @user
            random_number = @user.generate_random_number
            puts random_number
            new_random_number = "c" + @user.id.to_s + random_number
            @user.customerid = new_random_number
            @user.created_at = get_current_datetime
            # @user.updated_time = ''
            @user.save

            # save user's maximum access days
            user_validity = UserAccessValidity.new
            user_validity.user_id = self.current_user.id
            user_validity.maximum_access_in_days = 90
            user_validity.save 

            # redirect_back_or_default(:controller => '/account', :action => 'welcome')
            redirect_to :controller => '/account', :action => 'welcome'
            flash[:notice] = "<span class='success'>Thanks for registering!</span>".html_safe

        end
    end

注册.html.erb

<font color=red>(Fields marked * are mandatory)</font><h3>Sign me up!</h3>

<br>
<span class='error'><%= error_messages_for (@user) %></span>

<%= form_for :user do |f| -%>

<span class='error'><%= flash[:msg] %></span>

<p><label for="login"><span class='redcolor'>*</span>Login</label><br/>
<%= f.text_field :login %></p>


<p><label for="email"><span class='redcolor'>*</span>Email</label><br/>
<%= f.text_field :email %></p>

<p><label for="password"><span class='redcolor'>*</span>Password</label><br/>
<%= f.password_field :password %></p>

<p><label for="password_confirmation"><span class='redcolor'>*</span>Confirm Password</label><br/>
<%= f.password_field :password_confirmation %></p>

<p><label for="company"><span class='redcolor'>*</span>Company</label><br/>
<%= f.text_field :company %></p>


<p><%= f.submit 'Sign up', :name=> 'sign_up' %></p>

<% end -%>
4

1 回答 1

0

问题是您将一个if选项传递给您的验证,该选项也适用于存在验证。以以下为例

validates :login,
  :presence => true, 
  :length => { :within => 3..40}, 
  :uniqueness => { :case_sensitive => false },
  :format => { :with =>  /^([a-z_0-9\.]+)$/i },
  :on => :create, 
  :if => :is_login_entered?

is_login_entered?在验证存在与验证冲突的登录时调用,这就是跳过验证的原因。将您的验证更改为

validates :login, :presence => true, :on => :create
validates :login,
  :length => { :within => 3..40}, 
  :uniqueness => { :case_sensitive => false },
  :format => { :with =>  /^([a-z_0-9\.]+)$/i },
  :on => :create, 
  :if => :is_login_entered?

这样,其他验证将仅在存在登录时运行。

于 2013-04-17T12:26:36.743 回答