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

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

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

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

Ruby

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

Ruby on Rails 6

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

RubyGems

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

Ruby on Rails

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

Q&A

解決済

1回答

662閲覧

Routing Errorが解決できません

退会済みユーザー

退会済みユーザー

総合スコア0

Ruby on Rails 5

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

Ruby

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

Ruby on Rails 6

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

RubyGems

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/02/11 01:43

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

relationships_controller.rbを作っていませんでした。

投稿2020/02/11 02:36

退会済みユーザー

退会済みユーザー

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問