Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 788739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:24:56+00:00 2026-05-14T21:24:56+00:00

I am working on getting multiple file uploads working for an model in my

  • 0

I am working on getting multiple file uploads working for an model in my application, I have included the code below:

delivers_controller.rb

# POST /delivers

def create
  @deliver = Deliver.new(params[:deliver])

  process_file_uploads(@deliver)

  if @deliver.save
    flash[:notice] = 'Task was successfully created.'
    redirect_to(@deliver)
  else
    render :action => "new" 
  end
end

protected

def process_file_uploads(deliver)
    i = 0
    while params[:attachment]['file_'+i.to_s] != "" && !params[:attachment]['file_'+i.to_s].nil?
        deliver.assets.build(:data => params[:attachment]['file_'+i.to_s])
        i += 1
    end
end

deliver.rb

  has_many :assets, :as => :attachable, :dependent => :destroy

  validate :validate_attachments

    Max_Attachments = 5
    Max_Attachment_Size = 5.megabyte

    def validate_attachments
      errors.add_to_base("Too many attachments - maximum is #{Max_Attachments}") if assets.length > Max_Attachments
      assets.each {|a| errors.add_to_base("#{a.name} is over #{Max_Attachment_Size/1.megabyte}MB") if a.file_size > Max_Attachment_Size}
    end

assets_controller.rb

class AssetsController < ApplicationController

  def show
    asset = Asset.find(params[:id])
    # do security check here
    send_file asset.data.path, :type => asset.data_content_type
  end

  def destroy
      asset = Asset.find(params[:id])
      @asset_id = asset.id.to_s
      @allowed = Deliver::Max_Attachments - asset.attachable.assets.count 
      asset.destroy
    end

end

asset.rb

class Asset < ActiveRecord::Base
  has_attached_file :data,

  belongs_to :attachable, :polymorphic => true

  def url(*args)
    data.url(*args)
  end

  def name
    data_file_name
  end

  def content_type
    data_content_type
  end

  def file_size
    data_file_size
  end
end

Whenever I create a new deliver item and try to attach any files I get the following error:

NoMethodError in DeliversController#create

You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
/Users/danny/Dropbox/SVN/railsapps/macandco/surveymanager/trunk/app/controllers/delivers_controller.rb:60:in `process_file_uploads'
/Users/danny/Dropbox/SVN/railsapps/macandco/surveymanager/trunk/app/controllers/delivers_controller.rb:46:in `create'

new.html.erb (Deliver view)

<% content_for :header do -%>
    Deliver Repositories
<% end -%>

<% form_for(@deliver, :html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :caseref %><br />
    <%= f.text_field :caseref %>
  </p>
  <p>
    <%= f.label :casesubject %><br />
    <%= f.text_area :casesubject %>
  </p>


  <p>
    <%= f.label :description %><br />
    <%= f.text_area :description %>
  </p>

  <p>Pending Attachments: (Max of <%= Deliver::Max_Attachments %> each under <%= Deliver::Max_Attachment_Size/1.megabyte%>MB)
      <% if @deliver.assets.count >= Deliver::Max_Attachments %>
        <input id="newfile_data" type="file" disabled />
      <% else %>
        <input id="newfile_data" type="file" />
      <% end %>
      <div id="attachment_list"><ul id="pending_files"></ul></div>

  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

<%= link_to 'Back', delivers_path %>

Show.html.erb (Delivers view)

<% content_for :header do -%>
    Deliver Repositories
<% end -%>

<p>
  <b>Title:</b>
  <%=h @deliver.caseref %>
</p>
<p>
  <b>Body:</b>
  <%=h @deliver.casesubject %>
</p>

<p><b>Attached Files:</b><div id="attachment_list"><%= render :partial => "attachment", :collection => @deliver.assets %></div></p>

<%= link_to 'Edit', edit_deliver_path(@deliver) %> | <%= link_to 'Back', deliver_path %>

<%- if logged_in? %>
<%= link_to 'Edit', edit_deliver_path(@deliver) %> |
<%= link_to 'Back', delivers_path %>
<% end %>

_attachment.html.erb (Delivers view)

<% if !attachment.id.nil? %><li id='attachment_<%=attachment.id %>'><a href='<%=attachment.url %>'><%=attachment.name %></a> (<%=attachment.file_size/1.kilobyte %>KB)
<%= link_to_remote "Remove", :url => asset_path(:id => attachment), :method => :delete, :html => { :title  => "Remove this attachment", :id => "remove" } %></li>
<% end %>

I have been banging my head against the wall with the error all day, if anyone can shed some light on it, I would be eternally grateful!

Thanks,

Danny

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:24:57+00:00Added an answer on May 14, 2026 at 9:24 pm

    The error message indicates that params[:attachment] is nil inside process_file_uploads(), which causes params[:attachment]['file_'+i.to_s] to raise an exception.

    This happens because there’s no field named attachment in the form in new.html.erb.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on getting Jinja2 to work with Google AppEngine. I have the
I have been working on getting this seat mapping chart for a while and
I have been working on getting a mail app to work from a tutorial
I am working on getting familiar with the Android SDK and I have having
I have been working on getting a basic ATL project to compile in Visual
I am working on an API which returns me a zip file containing multiple
I'm uploading multiple files via XmlHTTPRequest and HTML5. I have the uploading working fine,
I have a working copy for my entire repository, which contains multiple Python projects.
I am doing Multiple file upload with Struts2. It was working fine I map
I've been working on getting this program complete where it saves multiple structs to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.