コントローラーに下のコードを挿入してからcurrent_accountを使おうとしましたが、NoMethodError in Accounts#show
Showing /home/ec2-user/environment/matching_app/app/views/accounts/_follow_form.html.erb where line #1 raised:
undefined method `current_account?' for #<#Class:0x00007fab18bcb0e0:0x00007fab18d0a370>
Did you mean? current_account
というエラーが出てきて困っています。ネットで調べているとこの手順でcurrent_accountを使えていますが、自分はできず困っています。宜しくお願いします。
before_action :authenticate_user!
accounts_controller.rb
class AccountsController < ApplicationController before_action :authenticate_account! def index @accounts = Account.all end def show @account = Account.find_by(id: params[:id]) end def following @account = Account.find(params[:id]) render 'show_follow' end def followers @account = Account.find(params[:id]) render 'show_follow' end end
show.html.erb
<h1><%= @account.id %></h1> <h1><%= @account.username %></h1> <h1><%= @account.email %></h1> <% @account ||= current_account %> <div class="stats"> <a href="<%= following_account_path(@account) %>"> <strong id="following" class="stat"> <%= @account.following.count %> </strong> following </a> <a href="<%= followers_account_path(@account) %>"> <strong id="followers" class="stat"> <%= @account.followers.count %> </strong> followers </a> </div> <%= render 'accounts/follow_form' %>
_follow.html.erb
<%= form_for(current_accounts.active_relationships.build) do |f| %> <div><%= hidden_field_tag :followed_id, @account.id %></div> <%= f.submit "Follow", class: "btn btn-primary" %> <% end %>
_follow_form.html.erb
<% unless current_account?(@account) %> <div id="follow_form"> <% if current_account.following?(@account) %> <%= render 'unfollow' %> <% else %> <%= render 'follow' %> <% end %> </div> <% end %>
account.rb
class Account < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower # ユーザーをフォローする def follow(other_account) following << other_account end # ユーザーをフォロー解除する def unfollow(other_account) active_relationships.find_by(followed_id: other_account.id).destroy end # 現在のユーザーがフォローしてたらtrueを返す def following?(other_account) following.include?(other_account) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。