2011-12-12 83 views
0

我們在將某些虛擬屬性從我的表單傳遞到正在測試的基本用戶名/密碼生成器時遇到了一些問題。將虛擬屬性傳遞給Rails3中的模型的問題

在它的第一個化身,我們要進入用戶名的號碼形式所需/密碼:

= simple_form_for(@user, :method => :put, :url => url_for({:controller => :users, :action => :new_batch_create})) do |f| 
    = f.text_field :usercount, :placeholder => 'Number of Passwords' 
    = f.submit 

在我們的用戶模型,我們有這樣的:

... 
attr_accessor :usercount 

def self.generate_batch 
    usercount.times do 
    username = "" 
    password ="" 
    5.times { username << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61))).chr } 
    5.times { password << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61))).chr } 
    User.create!(:username => username, :password => password) 
    end 
end 
... 

而在控制器:

def new_batch_create 
    @user = User.generate_batch(params[:user][:usercount]) 
    redirect_to root_path 
    end 

但是,當我們點擊提交,我們最終與ArgumentError:

wrong number of arguments (1 for 0) 

app/models/user.rb:22:in `generate_batch' 
app/controllers/users_controller.rb:38:in `new_batch_create' 

如果我們去掉(PARAMS [:用戶] [:USERCOUNT位])從控制器動作,我們得到這樣的:

undefined local variable or method `usercount' for #<Class:0x0000010177d628> 

如果我們嘗試:USERCOUNT位,我們得到這樣的:

undefined method `times' for :usercount:Symbol 

幫助!

回答

0

您對new_batch_create的定義沒有指定參數,但是調用具有。你需要把這些融合在一起。

def self.generate_batch(usercount) 
    usercount.times do 
    ... 
+0

@BDM這有幫助,謝謝。看起來它現在發送的字符串不是整數?未定義的方法'次'爲「12」:字符串 – simonmorley

+0

另外,我們如何發送多個參數。我試過這個:(usercount,username_l)但失敗了。我應該分成幾塊嗎? – simonmorley