Home>

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


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
https://qiita.com/seitarooodayo/items/c9d6955a12ca0b1fd1d4
-How to implement CSV/Excel/OpenOffice upload function in Rails
https://ruby-rails.hatenadiary.com/entry/20141120/1416483136

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

nil: NilClass article
・ How to handle "NoMethodError" of nil: NilClass
https://www.ryotaku.com/entry/2019/02/18/231458

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.

[routes.rb]

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'
    end
    resources: attendances, only:: update
  end


[users.rb]

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


[import.html.erb]

<% 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%>


  <p>Example</p>

<% 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);
  }
</script>
  • Answer # 1

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

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


  • Answer # 2

      

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

    Why do I need to redirect?

Related articles