Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 polymorphic dissociation "NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO"
    primarykey
    data
    text
    <p>I've two models with a polymorphic association (hopefully set up well). When I try to upload a file I run in an error witch tells me something like: <code>NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "uploads"</code> I have absolut no glue where the name attribute comes from and why the model saves if I left the file attribute empty. </p> <pre><code>class Event &lt; ActiveRecord::Base attr_accessible :title, :uploads_attributes has_many :uploads, :as =&gt; :uploadable accepts_nested_attributes_for :uploads end class Upload &lt; ActiveRecord::Base attr_accessible :filename, :path, :title belongs_to :uploadable, :polymorphic =&gt; true end </code></pre> <p>Here the form views for adding new events with uploads</p> <pre><code>&lt;%= form_for(@event) do |f| %&gt; &lt;div class="field"&gt; &lt;%= f.text_area :title, :rows =&gt; 4 %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.fields_for :uploads do |builder| %&gt; &lt;div&gt;&lt;%= builder.text_field :title %&gt;&lt;/div&gt; &lt;div&gt;&lt;%= builder.file_field :filename %&gt;&lt;/div&gt; &lt;% end %&gt; &lt;/div&gt; &lt;div class="actions"&gt; &lt;%= f.submit %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>The controller is straight forward like shown in Railscast episodes #196 and #197</p> <pre><code>def new @event = Event.new @event.uploads.build respond_to do |format| format.html # new.html.erb format.json { render json: @event } end end </code></pre> <p><strong>update:</strong> The create action is plain vanilla scaffolding code...</p> <pre><code>def create @event = Event.new(params[:event]) respond_to do |format| if @event.save format.html { redirect_to @event, notice: 'Event was successfully created.' } format.json { render json: @event, status: :created, location: @event } else format.html { render action: "new" } format.json { render json: @event.errors, status: :unprocessable_entity } end end end </code></pre> <p>If I just insert a title to the Upload form all things running well. But if I select a file as well I run in this error on saving.</p> <pre><code>NoMethodError: undefined method `name' for nil:NilClass: INSERT INTO "uploads" ("created_at", "filename", "path", "title", "updated_at", "uploadable_id", "uploadable_type") VALUES (?, ?, ?, ?, ?, ?, ?) </code></pre> <p>The parameters looking fine for me ...</p> <pre><code>{"utf8"=&gt;"✓", "authenticity_token"=&gt;"ppPQnkqXPSbNzRU4KGW11EpzktONZC2DS+hkRQAOnlM=", "event"=&gt;{"title"=&gt;"Erstes", "uploads_attributes"=&gt;{"0"=&gt;{"title"=&gt;"foo", "filename"=&gt;#&lt;ActionDispatch::Http::UploadedFile:0x00000003a2d548 @original_filename="Hazard_E.svg", @content_type="image/svg+xml", @headers="Content-Disposition: form-data; name=\"event[uploads_attributes][0][filename]\"; filename=\"Hazard_E.svg\"\r\nContent-Type: image/svg+xml\r\n", @tempfile=#&lt;File:/tmp/RackMultipart20120721-25352-1ioiss9&gt;&gt;}}}, "commit"=&gt;"Create Event"} </code></pre> <p>This is a Rails 3.2.6 app. I've create a new one with the same error as in my development project.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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