私はtwitterクローンに似たプロジェクトに取り組んでいます。しかし、私は次のようなエラーに遭遇しています:未定義のメソッド `following 'for nil:私のコントローラのNilClass。だから、開始するには、ここに私のコントローラである:定義されていないメソッド `nil 'のための` following':NilClass
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 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 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)
あなたは何にも 'current_user'を初期化していないようです。 – jktin12
私はcurrent_userがdeviseの一部であると思っていました。何も初期化する必要はありませんでしたか?私が間違っている?私は決してそれをしなかった他のプロジェクトでかなり確かですが、また、私は間違っている可能性があります。 –
あなたはユーザーがサインインしていることを確認しましたか? – Ren