{"id":655,"date":"2022-08-30T15:04:58","date_gmt":"2022-08-30T15:04:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/editing-multiple-records-granting-access-to-all-logged-in-users-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:58","modified_gmt":"2022-08-30T15:04:58","slug":"editing-multiple-records-granting-access-to-all-logged-in-users-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/editing-multiple-records-granting-access-to-all-logged-in-users-collection-of-common-programming-errors\/","title":{"rendered":"Editing Multiple Records &#8211; Granting Access to All Logged-In Users-Collection of common programming errors"},"content":{"rendered":"<p>As far as I can tell, this is a views and controller problem.<\/p>\n<p>I need to allow users to edit multiple records in one of my database tables. Railscast 165 deals with a similar situation, but it&#8217;s not quite on point for me: http:\/\/railscasts.com\/episodes\/165-edit-multiple<\/p>\n<p>I have a fixed set of products, and I need to let users add associated varieties to those products. I&#8217;ve considered the easier solution of requiring the user to go to a product page, and edit one individual product at a time, but that won&#8217;t work for my app. Instead, I need to provide more of an admin style functionality for this one task.<\/p>\n<p>The model relationship here is simple. A product <code>has_many<\/code> varieties, and variety <code>belongs_to<\/code> product. In my user interface, I simply let the user select a product from a dropdown menu containing all of the available products on the site, and then the user can type the name of a variety in a text input. For example, if we&#8217;re talking about apples, then the user can select apples from the product dropdown, and he might type in &#8220;fuji&#8221; and a variety. I simply need to save the <code>product_id<\/code> and the variety[name] to the varieties table. However, I need to do this for multiple records at one time.<\/p>\n<p>Currently, I&#8217;ve got the form rendering with the dropdown displaying the right product choices. The problem seems to be that the variety input is rendering in html like this:<\/p>\n<pre><code>Variety\n\n<\/code><\/pre>\n<p>To render parts of the form, I&#8217;m following the Advanced Rails Recipes multi-model form technique (i.e. the Ryan Bates way). I spent about 10 hours trying to do this and it seems that I&#8217;m now very close, but I don&#8217;t know what the problem is at this point.<\/p>\n<p>Here&#8217;s my setup:<\/p>\n<h2>Products Controller<\/h2>\n<pre><code>def edit_multiple\n  @products = Product.find(:all)\nend\n\n def update_multiple\n  params[:product][:existing_variety_attributes] ||= {}\n\n   @products = Product.find(:all)\n     if @products.each do |product|\n       product.update_attributes!(params[:product].reject { |k,v| v.blank? })\n     end\n     flash[:notice] = \"Updated products!\"\n     redirect_to edit_user_path(self.current_user)\n     else\n    render :action =&gt; 'edit'\n    flash[:error] = \"Something went wrong.  Please try again.\"\n  end\nend\n<\/code><\/pre>\n<pre><code>map.resources :products, :collection =&gt; { :edit_multiple =&gt; :get, :update_multiple =&gt; :put}\n<\/code><\/pre>\n<pre><code>\n update_multiple_products_path, :html =&gt; { :method =&gt; :put } do |f| %&gt;\n\n\n    'variety' %&gt;\n\n\n<br \/><br \/><br \/>\n\n\n\n<\/code><\/pre>\n<p>Note: For now, I&#8217;m not letting the users edit the varieties they submit. So I removed the following line from the Ryan Bates technique because it was throwing an error and I don&#8217;t think I need it, but I could be wrong:<\/p>\n<p>Here&#8217;s what I have in this _variety partial:<\/p>\n<pre><code>\n   \n        true}) %&gt;\n       \n       \n\n\n<\/code><\/pre>\n<p>NOTE: Currently the javascript stuff you see is working. On the edit_multiple view page, I can dynamically add and remove the products\/varieties inputs. As I mentioned, the dropdown is also populating properly. So it seems that I just need to get the variety input to render properly and get the controller to properly process it. Thanks for your help!<\/p>\n<h3>Update<\/h3>\n<p>When I select one product from the dropdown and type a name in the variety input, the submit throws the following error and trace:<\/p>\n<pre><code> ActiveRecord::UnknownAttributeError in ProductsController#update_multiple\nunknown attribute: product_id\n\n\/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-2.3.3\/lib\/active_record\/base.rb:2740:in `attributes='\n\/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-2.3.3\/lib\/active_record\/base.rb:2736:in `each'\n\/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-2.3.3\/lib\/active_record\/base.rb:2736:in `attributes='\n\/Library\/Ruby\/Gems\/1.8\/gems\/activerecord-2.3.3\/lib\/active_record\/base.rb:2628:in `update_attributes!'\n\/Users\/michael\/dev\/fresh\/app\/controllers\/products_controller.rb:74:in `update_multiple'\n<\/code><\/pre>\n<p>\/Users\/michael\/dev\/fresh\/app\/controllers\/products_controller.rb:73:in <code>each' \/Users\/michael\/dev\/fresh\/app\/controllers\/products_controller.rb:73:in<\/code> update_multiple&#8217;<\/p>\n<p>The log shows the correct product_id and the correct name I entered, but you can see it&#8217;s calling a nil class:<\/p>\n<pre><code>Processing ProductsController#update_multiple (for 127.0.0.1 at 2009-10-08 07:31:05) [PUT]\n  Parameters: {\"commit\"=&gt;\"Submit Varieties\", \"authenticity_token\"=&gt;\"zzkveSe7qzv2NY8WPrR2cYS376u6DBiz8Vc9iNFLQy8=\", \"product\"=&gt;{\"product_id\"=&gt;\"5\"}, \"nil_class\"=&gt;{\"name\"=&gt;\"yellow\"}}\n<\/code><\/pre>\n<p>When I try to enter more than one product\/variety record, the log isn&#8217;t recognizing anything except the first one. I get the same result as when I type in just one product\/variety record.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:11:09. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>As far as I can tell, this is a views and controller problem. I need to allow users to edit multiple records in one of my database tables. Railscast 165 deals with a similar situation, but it&#8217;s not quite on point for me: http:\/\/railscasts.com\/episodes\/165-edit-multiple I have a fixed set of products, and I need to [&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-655","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/655","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=655"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/655\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}