{"id":711,"date":"2022-08-30T15:05:54","date_gmt":"2022-08-30T15:05:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/adding-multipart-true-throws-undefined-method-name-error-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:05:54","modified_gmt":"2022-08-30T15:05:54","slug":"adding-multipart-true-throws-undefined-method-name-error-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/adding-multipart-true-throws-undefined-method-name-error-collection-of-common-programming-errors\/","title":{"rendered":"Adding :multipart =&gt; true throws Undefined Method &ldquo;name&rdquo; error-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m drawing a blank on this one. Here&#8217;s my problem:<\/p>\n<p><strong>Short Version<\/strong><\/p>\n<p>My <code>image#create<\/code> action is failing on the <code>image.save<\/code> call and throwing <code>Undefined method \"name\" for nil:NilClass<\/code>. The Image model includes a file upload via <code>paperclip<\/code> gem.<\/p>\n<p>The part that is throwing me:<\/p>\n<p><strong>the error only occurs when I include <code>:multipart =&gt; true<\/code> on the submitting form<\/strong><\/p>\n<p>When I take that out, the form works perfectly, except of course the image file doesn&#8217;t get uploaded. See the stack trace below for details on where it fails. It looks like its hitting a nil value when trying to quote something for the INSERT statement, but I can&#8217;t figure out what. My controller parameters look ok, and I checked via the logger to make sure the new <code>Image<\/code> instance is properly built just before it hits the save call.<\/p>\n<p><strong>Extra Details<\/strong><\/p>\n<p>I&#8217;ll try to include all the details I can think of, ordered by importance:<\/p>\n<ul>\n<li>\n<p>I&#8217;m using paperclip and attempting to store via S3, although this problem still occurs when I completely comment out the <code>has_attached_file<\/code> line in my Image model.<\/p>\n<\/li>\n<li>\n<p>Image is a nested resource inside Collection, which is referenced by a slug in its <code>to_param<\/code> method.<\/p>\n<\/li>\n<li>\n<p>I have the basics of a multi-tenant system (aka Basecamp-style user subdomains, implemented via a central user model that scopes all the components).<\/p>\n<\/li>\n<\/ul>\n<p><strong>Parameters passed to controller<\/strong><\/p>\n<pre><code>{\"utf8\"=&gt;\"\u00e2\u0153\u201c\",\n \"authenticity_token\"=&gt;\"JYCMky7851j5cW4ChSWUCCL\/02iePf6i\/QWAgR8q5tE=\",\n \"image\"=&gt;{\"name\"=&gt;\"My testerific image 2\",\n \"slug\"=&gt;\"my-testerific-image-2\",\n \"description\"=&gt;\"w\",\n \"collection_id\"=&gt;\"2\",\n \"sort\"=&gt;\"3\",\n \"picture_file_name\"=&gt;#', \n        #   :full =&gt; '800x800&gt;' \n        #},\n        :storage =&gt; :s3,\n        :s3_credentials =&gt; Rails.root.join( 'config', 's3.yml' ),\n        :path =&gt; \":id\/:style\/:filename\"\n\n\n  def to_param\n    self.name.parameterize\n  end\n  private\n  def create_slug\n    self.slug = self.to_param\n  end\nend\n<\/code><\/pre>\n<p>I compared the <code>Image<\/code> instance that was built with <code>:multipart =&gt; true<\/code> vs without it, and confirmed the only difference is the file upload field, <code>picture_file_name<\/code>. Without <code>:multipart =&gt; true<\/code>, it a string with the filename (not surprising). With it, it is an instance of <code>ActionDispatch::Http::UploadedFile<\/code>. Given that the stack trace shows its failing during a <code>to_yaml<\/code> call, perhaps the problem lies with the <code>UploadedFile<\/code> instance converting to YAML?<\/p>\n<ol>\n<li>\n<p>Well, I finally buckled down and traced my way through the ActiveRecord source code via logger outputs. Turns out, as I suspected, it was failing to save the <code>UploadedFile<\/code> because it couldn&#8217;t convert it to YAML.<\/p>\n<p>However, this because of a stupid mistake on my part. In my submitting form, I had:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>When it should have been:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>The <code>paperclip<\/code> gem understands that <code>:picture<\/code> is the uploaded file and works the background magic to save the file name to the proper field. I just underestimated the ease of use I guess!<\/p>\n<p>Normally, I would delete the question for something this trivial, but seeing as I made this mistake, others probably will in the future, and there was very little out there to point me to the solution. So I will keep it up to aid in the future googling of similarly dense developers!<\/p>\n<\/li>\n<li>\n<p>After lot of searching and debugging. The main symptom of this problem is that form with a file field are being made as multipart form and rails 3.2 is not able to save the data because it is trying to save other parameters that are not present in your database (The filed for the file object).<\/p>\n<p>Taking the appopriate values and saving them to some variable and removing the file hash from the params has seems to work around the issue!<\/p>\n<p>Here is an example of a working code to save in the database using rails 3.2<\/p>\n<pre><code>file = params[:document][:file].tempfile.read\nparams[:document].delete(:file)\n@document = Document.new(params[:document])   \nrespond_to do |format|\n  if @document.save\n@document.file = file\n@document.save\n    format.html { redirect_to @document, notice: 'Document was successfully created.' }\n    format.json { render json: @document, status: :created, location: @document }\n  else\n    format.html { render action: \"new\" }\n    format.json { render json: @document.errors, status: :unprocessable_entity }\n  end\nend\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You didn&#8217;t post it, but I think you should go with <code>:html =&gt; { :multipart =&gt; true }<\/code> instead of just <code>:multipart =&gt; true<\/code> in your form, like:<\/p>\n<pre><code>form_for object, :html =&gt; { :multipart =&gt; true } do |o|\n    ...\n<\/code><\/pre>\n<\/li>\n<li>\n<p>I had spent about a week on the same error, even though I had the correct fields.<\/p>\n<p>The silliness causing all my headaches was simply the order of tags in the view.<\/p>\n<p><strong>Caused Error<\/strong><\/p>\n<p><code>NoMethodError in DatenightsController#create undefined methodname' for nil:NilClass<\/code>:<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p><strong>Solution for above quoted Error<\/strong> (Just the sequence\/order matters a lot here)<\/p>\n<pre><code>\n\n<\/code><\/pre>\n<p>Since the issue took so much of my sweet time I thought it may help you.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:42:50. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m drawing a blank on this one. Here&#8217;s my problem: Short Version My image#create action is failing on the image.save call and throwing Undefined method &#8220;name&#8221; for nil:NilClass. The Image model includes a file upload via paperclip gem. The part that is throwing me: the error only occurs when I include :multipart =&gt; true on [&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-711","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/711","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=711"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/711\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=711"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=711"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=711"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}