2011-12-12 161 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