Home > OS >  Rails 6 : NoMethodError and undefined method errors
Rails 6 : NoMethodError and undefined method errors

Time:11-20

I am trying to add a post action button to upload a CSV to get data from CSV into postGreSQL database. But getting the following error(copied the error from request response of chrome devtools)

NoMethodError in Admin::UnitsController#import

undefined method `content_type' for nil:NilClass
Extracted source (around line #15):

#13     file = params[:file]
#14     
*15     return redirect_to import_admin_tenant_site_units_path, notice: 'Only CSV please' unless file.content_type == "text/csv"

I have tried the following which is giving me the error.

Following the routes.rb

  namespace :admin do
    get '', to: 'dashboard#index', as: 'root'
    # resourceful routes 
    resources :oauth_clients
    resources :tenants do
      resources :sites do
        #resources :production_shifts
        resources :units do
          resources :log_data_fields, only: [:create, :index]
            # collection route 
            collection do 
              post :import #post action 
            end
          member do
            get :logs
            get :export_log
          end
          resources :reports
          resources :grafana_dashboards
          resources :data_adapters, except: [:show]
          get 'data_adapters/start'
        end
      end
      resources :users
      resources :login_activities
      # resources :login_activities
    end
  end

where post action "import" is of concern currently for this error.

I have included the import method's logic in the units_controller.rb as follows :

class Admin::UnitsController < Admin::BaseController

  # import request(this is gonna be a POST action)
  def import
    logger.debug("*****Testing the logger.*****")
    
    file = params[:file]
    
    return redirect_to import_admin_tenant_site_units_path, notice: 'Only CSV please' unless file.content_type == "text/csv"
    
    file = File.open(file)
    csv = CSV.parse(file, headers: true) 
    
    binding.b
    
    redirect_to import_admin_tenant_site_units_path, notice:"Imported tags !"
  end

More things are to be done and I cannot even show complete code in public due to restricted reasons. My rails debugging gem debug doesnt work either means it doesnt gets invoked even after mentioning it there as binding.b. Earlier it was working few days back but I dont know what mistakes I did. And Sorry for poor explaining language.

Here I am also showing the frontend view code of the part where the csv file gets uploaded from. importtags.html.haml :

%p{:style => "color: green"}= notice

= form_with model:@log_data_field, url: import_admin_tenant_site_units_path, method: :post do |form|

    - if @log_data_field.errors.any?
        #error_explanation
            %h2= "#{pluralize(@log_data_field.errors.count, "error")} prohibited this log_data_field from being saved:"
            %ul
            - @log_data_field.errors.full_messages.each do |message|
                %li= message

    = form.file_field :file, accept: ".csv"
    <br>
    <br>

    -#button.btn.primary{:type => "submit", data: { disable_with: "Please wait..."}}
    %button.btn.primary{:type => "submit"}
        -#= form.button "Import"
        = "Import"

PS : -# are indicating comments in the above haml code.

CodePudding user response:

params[:file] is nil so the local file variable is going to be nil as well.

In the rails log you can see the params hash usually after Started POST and Processing by lines like Parameters: {...} But you can always just use puts inside the controller to debug like p params to see its content.

I bet the file key inside the params hash is going to be nested under the name of your model.

Try file = params[:YOUR_MODEL_NAME][:file]

Maybe file = params[:log_data][:file]?:D

  • Related