未定义的方法`以下'为零:NilClass

时间:2016-08-11 17:38:35

标签: ruby ruby-on-rails-4 devise

我正在开发一个类似于twitter克隆的项目。但是,我遇到了以下错误:未定义的方法`跟随'为nil:我的控制器中的NilClass。所以,首先,这是我的控制器:

class EpicenterController < ApplicationController

def feed
 #Here we initialize the array that will hold tweets from the current_user's    
  following list.
 @following_tweets = []

  Tweet.all.each do |tweet|
    if current_user.following.include?(tweet.user_id)
     @following_tweets.push(tweet)
    end 
  end
end

def show_user
  @user = User.find(params[:id])
end

def now_following
  @user = User.find(params[:follow_id])

  #Adding the user.id of the user you want to follow to your 'follow' array  
  attribute
  current_user.following.push(params[:follow_id].to_i)

  current_user.save
end

def unfollow
end

这是我的模特:

class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable

  serialize :following, Array       
  has_many :tweets

  validates :username, presence: true, uniqueness: true
end

我的架构:

ActiveRecord::Schema.define(version: 20160811164903) do

create_table "tweets", force: :cascade do |t|
  t.string   "message"
  t.integer  "user_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
  t.string   "email",                  default: "", null: false
  t.string   "encrypted_password",     default: "", null: false
  t.string   "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.integer  "sign_in_count",          default: 0,  null: false
  t.datetime "current_sign_in_at"
  t.datetime "last_sign_in_at"
  t.string   "current_sign_in_ip"
  t.string   "last_sign_in_ip"
  t.datetime "created_at",                          null: false
  t.datetime "updated_at",                          null: false
  t.string   "name"
  t.string   "username"
  t.text     "bio"
  t.string   "location"
  t.text     "following"
 end

 add_index "users", ["email"], name: "index_users_on_email", unique: true
 add_index "users", ["reset_password_token"], name:    
 "index_users_on_reset_password_token", unique: true

end

最后我的应用程序控制器:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters

    devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :username,   
    :bio,     
    :location, :following])

   devise_parameter_sanitizer.permit(:account_update, keys: [:name,   
   :username, :bio, :location, :following])
  end
end

感谢您提供的任何帮助。如果您需要查看其他内容,请告诉我,我会添加它。

堆栈追踪:

NoMethodError (undefined method `following' for nil:NilClass):
app/controllers/epicenter_controller.rb:8:in `block in feed'
app/controllers/epicenter_controller.rb:7:in `feed'


 Rendered /Users/mikecuddy/.rvm/rubies/ruby-  
 2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-  
 4.2.6/lib/action_dispatch/middleware/templates/rescues/_source.erb (3.9ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-
 4.2.6/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb    
 (2.7ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-  
 2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-  
 4.2.6/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.9ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/actionpack-  
 4.2.6/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb   
 within rescues/layout (63.4ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-  
 2.3.0/lib/web_console/templates/_markup.html.erb (0.4ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-  
 2.3.0/lib/web_console/templates/_inner_console_markup.html.erb within   
 layouts/inlined_string (0.3ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-   
 2.3.0/lib/web_console/templates/_prompt_box_markup.html.erb within    
 layouts/inlined_string (0.3ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-   
 2.3.0/lib/web_console/templates/style.css.erb within layouts/inlined_string   
 (0.5ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-  
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-  
 2.3.0/lib/web_console/templates/console.js.erb within layouts/javascript   
(62.0ms)
 Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
 2.2.1/lib/ruby/gems/2.2.0/gems/web-console-
 2.3.0/lib/web_console/templates/main.js.erb within layouts/javascript   
(0.3ms)
Rendered /Users/mikecuddy/.rvm/rubies/ruby-  
2.2.1/lib/ruby/gems/2.2.0/gems/web-console-
2.3.0/lib/web_console/templates/error_page.js.erb within layouts/javascript 
(0.6ms)
Rendered /Users/mikecuddy/.rvm/rubies/ruby-   
2.2.1/lib/ruby/gems/2.2.0/gems/web-console-  
2.3.0/lib/web_console/templates/index.html.erb (139.5ms)

1 个答案:

答案 0 :(得分:0)

对于未登录的用户,

current_usernil,因此您需要对其进行处理:

if user_signed_in?
  current_user.following # ...
else
  store_location_for :user, request.path
  redirect_to new_user_session_path, alert: 'You need to be logged in to do that.'
end

class EpicenterController < ApplicationController
  before_action :authenticate_user! #, only: [:feed, :now_following] (optional)
  # ...
end

如果您的控制器主要用于登录用户(看起来像这样)。