Problems Passing Virtual Attributes to Model in Rails3-Collection of common programming errors

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  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 #

And if we try :usercount, we get this:

 undefined method `times' for :usercount:Symbol

Help!

  1. 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
      ...
    

Originally posted 2013-11-09 19:42:45.