{"id":438,"date":"2022-08-30T15:01:21","date_gmt":"2022-08-30T15:01:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/problems-passing-virtual-attributes-to-model-in-rails3-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:21","modified_gmt":"2022-08-30T15:01:21","slug":"problems-passing-virtual-attributes-to-model-in-rails3-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/problems-passing-virtual-attributes-to-model-in-rails3-collection-of-common-programming-errors\/","title":{"rendered":"Problems Passing Virtual Attributes to Model in Rails3-Collection of common programming errors"},"content":{"rendered":"<p>We&#8217;re having some problems passing some virtual attributes from my form to a basic username \/ password generator we&#8217;re testing.<\/p>\n<p>In its first incarnation, we want to enter the number of usernames \/ password required in the form:<\/p>\n<pre><code>= simple_form_for(@user, :method =&gt; :put, :url =&gt; url_for({:controller =&gt; :users, :action =&gt; :new_batch_create})) do |f|\n  = f.text_field :usercount, :placeholder =&gt; 'Number of Passwords'\n  = f.submit\n<\/code><\/pre>\n<p>In our user model, we have this:<\/p>\n<pre><code>...\nattr_accessor :usercount  \n\n def self.generate_batch\n     usercount.times do\n     username = \"\"\n     password =\"\"\n     5.times { username  password)\n    end\n end\n ...\n<\/code><\/pre>\n<p>And in the controller:<\/p>\n<pre><code>  def new_batch_create\n   @user = User.generate_batch(params[:user][:usercount])\n   redirect_to root_path\n  end\n<\/code><\/pre>\n<p>But when we hit submit, we end up with an ArgumentError:<\/p>\n<pre><code> wrong number of arguments (1 for 0)\n\n app\/models\/user.rb:22:in `generate_batch'\n app\/controllers\/users_controller.rb:38:in `new_batch_create'\n<\/code><\/pre>\n<p>If we remove (params[:user][:usercount]) from the controller action, we get this:<\/p>\n<pre><code> undefined local variable or method `usercount' for #\n<\/code><\/pre>\n<p>And if we try :usercount, we get this:<\/p>\n<pre><code> undefined method `times' for :usercount:Symbol\n<\/code><\/pre>\n<p>Help!<\/p>\n<ol>\n<li>\n<p>Your definition of new_batch_create does not have a parameter specified, but the call does. You need to bring those into harmony.<\/p>\n<pre><code>def self.generate_batch(usercount)\n  usercount.times do\n  ...\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:42:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>We&#8217;re having some problems passing some virtual attributes from my form to a basic username \/ password generator we&#8217;re testing. In its first incarnation, we want to enter the number of usernames \/ password required in the form: = simple_form_for(@user, :method =&gt; :put, :url =&gt; url_for({:controller =&gt; :users, :action =&gt; :new_batch_create})) do |f| = f.text_field [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-438","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/438","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=438"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/438\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=438"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=438"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=438"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}