質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2073閲覧

devise gem で current_userが使えません

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

Ruby on Rails 5は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RubyGems

RubyGemsはRubyによるプログラミングのためのパッケージマネジメントツールです。ユーザはこれを使用することで、Rubyライブラリのダウンロードやアップデートや、依存関係の自動解決が可能になります。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/02/10 10:53

コントローラーに下のコードを挿入してから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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

current_account?のメソッドをヘルパーに記述したらできました。

def current_account?(account) account == current_account end

投稿2020/02/10 23:30

退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問