NoMethodError in UsersController # import
undefined method `import 'for #<Class: 0x00007f61f4ba6708>
Extracted source (around line # 18):
17 def import
18 User.import (params [: file])
19 redirect_to user_import_path @user
20 end

csvI am in trouble without getting out of the above error while implementing the import function.

[I want to realize]
・ Improved that the above error is thrown when transitioning to the user list.
・ Fromundefined method path for nil: NilClass, it can be read that thefilemethod is empty, but I want to get rid of the situation where the solution method is unknown.
・ Completed csv function implementation.

[I have tried it]
・ From the following article, I checked what the reference code and theundefined methodare telling us in the first place.
viewsfile_field_tag ​​filefromimport_users_pathJump to code>importaction and separate processing by view and model. ← Is this area suspicious? I think that some controllers (User.import (params [: file])) and models (self.import (file))fileI tried to fix the part, but the situation has not changed.

[Reference article]
Csv import feature article
・ 【Ruby on Rails】 CSV import
-How to implement CSV/Excel/OpenOffice upload function in Rails

Article about undefined method
・ Solution of Ruby error message undefined method [beginners]

nil: NilClass article
・ How to handle "NoMethodError" of nil: NilClass

It seems very rudimentary.
However, although my lack of power was revealed, I couldn't solve it but I asked you to lend me your help.
I hope you will study.


resources: users do
    collection {post: import}
    get 'import', to: 'users # import'
    get 'index_attendance', to: 'users # index_attendance'
    member do
      get 'edit_basic_info'
      patch 'update_basic_info'
      get 'attendances/edit_one_month'
      patch 'attendances/update_one_month'
      get 'attendances/edit_overtime_app'
      patch 'attendances/update_over_app'
    resources: attendances, only:: update


class User</pre>
<p><br />
<pre><code data-language = "Ruby">class UsersController " + @ user.errors.full_messages.join ("<br>")
    redirect_to users_url
  def admin_or_correct_user
    unless current_user? (@ user) || current_user.admin?
      flash [: danger] = "I was sorry!"
      redirect_to (root_url)
    def user_params
      params.require (: user) .permit (: name,: email,: affiliation,: employee_number,: password)
    def basic_params
      params.require (: user) .permit (: basic_time,: work_time)


<% provide (: title, "user list")%>
 User list 
<% if flash [: notice]%>
  <% = flash [: notice]%>
<% end%>

  <% = form_tag import_users_path, multipart: true do%>
    <% = file_field_tag: file%>
    <% = submit_tag "Import"%>
  <% end%>


<% if current_user.admin?%>
      <span>|</span><% = link_to "delete", "#", method:: delete, class: "btn btn-lg, btn-primary btn-delete"%>
      <% = link_to "edit", "#", class: "btn btn-lg btn-primary w-10"%>
<% end%>
<script type = "text/javascript">
  function file_selected (file_field) {
    var filename = $(file_field) [0] .files [0] .name;
    $("# filename"). val (filename);
  • Answer # 1

    This is an error when a CSV file is not attached.

    unless file
     #Process when there is no file attachment
     # Process when attached. Put the current code here

  • Answer # 2


    def import
          User.import (params [: file])
          redirect_to user_import_path @user

    Why do I need to redirect?

Related articles