简体   繁体   中英

NoMethodError undefined method `[]' for nil:NilClass

I'm have a problem. When create follow author, I'm getting error:

NoMethodError in FollowsController#create_author

undefined method `[]' for nil:NilClass

Here is the route

Rails.application.routes.draw do
 resources :follows, only: %i(create destroy create_author destroy_author)
 post "/follows/create_author", to: "follows#create_author", as: :create_author
 delete "/follows/destroy_author/:id", to: "follows#destroy_author", as: :destroy_author
end

Here is the form

<%= form_for(@follow, as: :create_author, url: create_author_path, remote: true) do |f| %>
  %= f.hidden_field :user_id, value: current_user.id if current_user != nil %>
  <%= f.hidden_field :type_follow, value: "author" %>
  <%= f.hidden_field :follower_id, value: @author.id %>
  <%= f.submit t(".submit"), id: "follow", class: "btn btn-primary" %>

Here is the controller enter link controller here

Help please :( Thanks!

When posting an error like this it would help us figure it out if you would include the line number and at least some of the stack trace. Without that we won't have much to go on to help.

That said, the one place you use [] in your controller is at the bottom in the find_user and find_author methods. Specifically you do params[:follow][:follower_id] . If params[:follow] is nil, then the error you get would make sense.

Couple of things to do..

  • Verify in your rails log that the parameters you think are being sent to your controller are in fact being sent.

  • Adjust those two methods to use follow_params not params .

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM