Anna
Anna

Reputation: 203

undefined method `original_filename' for nil:NilClass in active_admin

although I saw this problem in other blogs, I couldn't make it works in my case. My view:

<%= semantic_form_for :routes_status_race, :url => status_race_admin_routes_path do |f| %>
    <% contact_array = Contact.all.map {|contact| [contact.name, contact.id]}%>
    <% route_importer_array = RouteImporter.all.map {|importer| [importer.name, importer.id]}%>
    <%= f.inputs do%>
        <%= f.input :contact, :as => :select, :collection => Contact.all %>
        <%= f.input :route_import, :as => :select, :collection => RouteImporter.all %>
        <%= f.input :uploaded_data, :action => :status_race, :multipart => true, :as => :file %>
    <%end%>
    <input name='commit' type='submit' method='post' value='Import'/>
<%end%>

My controller:

collection_action :status_race, :method => :post do

    uploaded_io = params[:uploaded_data].original_filename

    File.open(Rails.root.join('app/importers', 'uploads', uploaded_io.original_filename), 'w') do |file|
      file.write(uploaded_io.read)
    end

    RoutesQuickcom.new.run(uploaded_io.original_filename)
    render "status_race"
  end

I get this error:undefined method `original_filename' for nil:NilClass in my controller and I don't know how to fix it, any help will be appreciate, thanks!

Upvotes: 1

Views: 3412

Answers (1)

Thanh
Thanh

Reputation: 8604

You called original_filename 3 times, remove two original_filename and it will works:

collection_action :status_race, :method => :post do

  uploaded_io = params[:uploaded_data].original_filename

  File.open(Rails.root.join('app/importers', 'uploads', uploaded_io), 'w') do |file|
    file.write(uploaded_io.read)
  end

  RoutesQuickcom.new.run(uploaded_io)
  render "status_race"
end

Upvotes: 2

Related Questions