Note that there are some explanatory texts on larger screens.

plurals
  1. PORails upload with nested forms
    text
    copied!<p>When I try to submit a simple nested form Rails give me an error:</p> <pre><code>NoMethodError in UserController#update undefined method `[]' for #&lt;ActionDispatch::Http::UploadedFile:0x3f4e318&gt; </code></pre> <p>And params are:</p> <pre><code>{ "utf8"=&gt;"✓", "authenticity_token"=&gt;"HFWawKp4RH7+AFV0yQ1cXpzxHDfubKTKkiDiS6QKnJk=", "user"=&gt; { "name"=&gt;"Alex", "pics_attributes"=&gt; { "pic"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x3f4e318 @original_filename="Beautiful Sky_thumb.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[pics_attributes][pic]\"; filename=\"Beautiful Sky_thumb.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#&lt;File:C:/Users/Alex/AppData/Local/Temp/RackMultipart20120824-4784-5rmxid&gt;&gt; } }, "commit"=&gt;"Save User" } </code></pre> <p>UserController#update</p> <pre><code>def update @user = User.new(params[:user]) @user.save end </code></pre> <p>Form (It uses generic form builder. Maybe that causes an error?)</p> <pre><code>&lt;%= form_for :user, url: '/user/update', html: {multipart: true} do |user| %&gt; &lt;%= user.hidden_field :name, {value: 'Alex'} %&gt; &lt;%= user.fields_for :pics_attributes do |pic| %&gt; &lt;%= pic.file_field :pic %&gt; &lt;% end %&gt; &lt;%= user.submit %&gt; &lt;% end %&gt; </code></pre> <p>User model</p> <pre><code>class User &lt; ActiveRecord::Base attr_accessible :name, :pics_attributes has_many :pics accepts_nested_attributes_for :pics end </code></pre> <p>Pics model</p> <pre><code>class Pics &lt; ActiveRecord::Base belongs_to :user attr_accessible :image mount_uploader :image, PicUploader end </code></pre> <p>Where to dig for an error?</p> <p>The code above is for Carrierwave, but I tried to switch to Paperclip and errors were the same.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload