表題の件で、質問いたします。
現在の開発環境
ruby 2.3.0p0 Rails 5.0.6
やりたいこと(方向性)
follow機能を実装しています。
質問したいこと
表題のエラーを解決方法
①について
users/_unfollow.html.erb(以下は最終的にshow.html.erbで表示されます。)
<%= form_for(current_user.active_relationships.find_by(following_id: @user.id), html:{method: :delete}, remote: true) do |f| %> <%= f.submit "フォローを解除", class: "btn btn-large follow-btn" %> <% end %>
解決について幾つか調べましたが、コントローラー側での@userの設定を指摘されているサイトが多かったのですが、それでは解決しませんでした。
users_controller.rb
def show @user = User.find(params[:id]) end
関係あるかどうかはわかりませんが、念のためmodels/user.rbも記載します。
# 自分がフォローした人を取り出す。 has_many :active_relationships,class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :followings, through: :active_relationships, source: :following # 自分のfollowerを取り出す。 has_many :passive_relationships,class_name: "Relationship", foreign_key: "following_id", dependent: :destroy has_many :followers, through: :passive_relationships, source: :follower # ユーザーをフォローする def follow(other_user) active_relationships.create(following_id: other_user.id) end # ユーザーをアンフォローする def unfollow(other_user) active_relationships.find_by(following_id: other_user.id).destroy end # 現在のユーザーがフォローしてたらtrueを返す def followings?(other_user) following.include?(other_user) end
以上、何卒よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。