2011-03-30 57 views
0

我想生成一個唯一的令牌,使用此教程:http://snippets.dzone.com/posts/show/6434令牌是Rails核心方法嗎? - Rails的3

但是當我嘗試我收到此錯誤信息:

undefined method `token=' for #<Stage:0x00000102a13aa8> 

這就是我的舞臺模型的樣子:

# == Schema Information 
# Schema version: 20110131093541 
# 
# Table name: stages 
# 
# id   :integer   not null, primary key 
# project_id :integer 
# user_id :integer 
# name  :string(255) 
# stage_num :integer 
# created_at :datetime 
# updated_at :datetime 
# 


class Stage < ActiveRecord::Base 

    belongs_to :project 
    #has_and_belongs_to_many :users 

    has_many :uploads, :dependent => :destroy, :order => 'created_at DESC' 
    has_many :comments, :dependent => :destroy 

    #this is where the token is generated for the client 
    def generate_client_token(length=25) 
    alphanumerics = ('a'..'z').to_a.concat(('A'..'Z').to_a.concat(('0'..'9').to_a)) 
    self.token = alphanumerics.sort_by{rand}.to_s[0..length] 

     #Ensure uniqueness of the token.. 
     generate_client_token unless Stage.find_by_token(self.token).nil?  
    end 

end 

在我看來,我打電話像這樣的方法:

<%= @stage.generate_client_token %> 

想法?

回答

3

您尚未在stages表中定義token字段。您可以通過生成具有此遷移做到這一點:

add_column :stages, :token, :string 

或者簡單地運行已經有這個遷移(如果有的話)