2013-03-25 213 views
0

我是新的紅寶石和語法!但我不斷收到這個錯誤,有人能告訴我爲什麼嗎?我也嘗試將它包含在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 

回答

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 
+0

當我插入到我的模型中我不斷收到「堆棧層次太深」? – jesper 2013-03-25 16:40:13

+0

你能展示你的型號代碼嗎? – codeit 2013-03-25 16:41:57

+0

什麼是使用'活動模型'或'活動記錄'? – codeit 2013-03-25 16:44:20

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 wil l創建一個用戶對象不是持續在數據庫中

+0

謝謝,我做到了。 – jesper 2013-03-25 17:06:35