We're having some problems passing some virtual attributes from my form to a basic username / password generator we're testing.
In its first incarnation, we want to enter the number of usernames / password required in the form:
= 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
In our user model, we have this:
...
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
...
And in the controller:
def new_batch_create
@user = User.generate_batch(params[:user][:usercount])
redirect_to root_path
end
But when we hit submit, we end up with an 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'
If we remove (params[:user][:usercount]) from the controller action, we get this:
undefined local variable or method `usercount' for #<Class:0x0000010177d628>
And if we try :usercount, we get this:
undefined method `times' for :usercount:Symbol
Help!
Your definition of new_batch_create does not have a parameter specified, but the call does. You need to bring those into harmony.
def self.generate_batch(usercount)
usercount.times do
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.