http:// localhost:3000 / users / new路由错误

时间:2015-02-27 11:32:46

标签: ruby-on-rails ruby ruby-on-rails-3

我遵循本教程。 http://www.sitepoint.com/rails-userpassword-authentication-from-scratch-part-i/我只是标题为向用户模型添加一些验证

的部分

当我转到http://localhost:3000/users/new时,我收到路由错误。但根据我的知识,溃败看起来很好。怎么了?

enter image description here

user_controller.rb

class UsersController < ApplicationController
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:user])
    if @user.save
      flash[:notice] = "You signed up successfully"
      flash[:color]= "valid"
    else
      flash[:notice] = "Form is invalid"
      flash[:color]= "invalid"
    end
    render "new"
  end
end

的routes.rb

Rails.application.routes.draw do
  get 'users/new'

模型user.rb

class User < ActiveRecord::Base
  attr_accessor :password
  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates :username, :presence => true, :uniqueness => true, :length => { :in => 3..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
end

1 个答案:

答案 0 :(得分:3)

将实施UsersController的文件重命名为users_controller.rb

此外,EMAIL_REGEX类中的正则表达式User会导致错误。你应该修改它,因为错误说:

EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i