我是ruby的新手,喜欢语法!但我一直得到这个错误,有人可以告诉我为什么?我也尝试将它包含在ActiveModel :: Validations中而没有任何运气。获得这个“堆栈级别太深”
class HomeController < ApplicationController
def index
if params[:username]
l = Users.new(:username => params[:username], :password => params[:password], :email => params[:email]).save
z = Users.where(:username => params[:username]).limit(1).last
@debugging = "Howdy" + z[:username] + ""
end
end
end
用户模型:
class Users < ActiveRecord::Base
validates :username, :presence => true
attr_accessible :email, :password, :username
end
答案 0 :(得分:1)
如果您使用active record
:
validates :username, :presence => true
应为User < ActiveRecord::Base
:
<强> user.rb 强>
class User < ActiveRecord::Base
validates :username, :presence => true #or you can also write as 'validates_presence_of :username'
attr_accessible :email, :password, :username
end
<强> home_controller.rb 强>
class HomeController < ApplicationController
def index
if params[:username]
l = User.new(:username => params[:username], :password => params[:password], :email => params[:email])
if l.save?
flash[:success] = "Valid user"
else
flash[:error] = "Invalid user"
end
z = User.where(:username => params[:username]).limit(1).last
@debugging = "Howdy" + z[:username] + ""
end
end
end
答案 1 :(得分:0)
您的模型似乎没问题
尝试为此更改索引操作:
if params[:username]
user = User.create(:username => params[:username], :password => params[:password], :email => params[:email])
if user.persisted?
@debugging = "Howdy #{user.username}"
else
@debugging = "Error while creating user"
end
end
如果用户无效 User.create 将在数据库中创建一个非持久的用户对象