Routing Error uninitialized constant RelationshipsController が解決できません。
友達一覧からフォロー、アンフォローのボタンを押した時にエラーが発生します。一通りネットにあるエラーを漁りましたが、できなかったので投稿させていただきます。宜しくお願いします。
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
routes.rb
Rails.application.routes.draw do devise_for :accounts get 'static_pages/home' # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root "static_pages#home" get "/home" => "static_pages#home" get "/howto" => "static_pages#howto" get "/accounts/sign_up" => "devise/registrations#new" get "/accounts/index" => "accounts#index" get "/accounts/:id", to: "accounts#show", as: "profile" resources :accounts do member do get :following, :followers end end resources :relationships, only: [:create, :destroy] 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_account.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
accounts_helper.rb
module AccountsHelper def current_account?(account) account == current_account end end
_unfollow.html.erb
<%= form_for(current_account.active_relationships.find_by(followed_id: @account.id), html: { method: :delete }) do |f| %> <%= f.submit "Unfollow", class: "btn" %> <% end %>
relationship.rb
class Relationship < ApplicationRecord validates :follower_id, presence: true validates :followed_id, presence: true belongs_to :follower, class_name: "Account" belongs_to :followed, class_name: "Account" end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。