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

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

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

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

Q&A

解決済

1回答

446閲覧

フォロー機能の作成 エラーが分かりません

ooaai

総合スコア17

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/02 12:19

編集2021/01/02 12:21

写真のようなエラーが出ています。
ログイン機能のある本の感想が投稿できるアプリを作成しています。フォロー機能を付けようと思い、試行錯誤しているのですがエラーが解決できず息詰まっています。初めてなので記事を見ながら記述しているのですがエラーが出てしまいました。調べてみたのですが解決に繋がりそうな記事が見付からずずっとこの状態です。エラーに関係ありそうなコードは添付したので解決策もしくはアドバイスいただけると嬉しいです。

イメージ説明
schema.rb↓(一部)

create_table "relationships", force: :cascade do |t| t.integer "fllower_id" t.integer "fllowed_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false end

User#index↓

<div class='container px-5 px-sm-0'> <div class='row'> <div class='col-md-3'> <h2>User info</h2> <%= render 'info', user: current_user %> <h2 class="mt-3">New book</h2> <%= render 'books/form', book: @book %> </div> <div class='col-md-8 offset-md-1'> <h2>Users</h2> <%= render 'index', users: @users %> </div> <div> </div>

users/_index.html.erb↓

<table class='table'> <thead> <tr> <th>image</th> <th>name</th> <th colspan="3"></th> </tr> </thead> <tbody> <% users.each do |user| %> <tr> <td><%= attachment_image_tag(user, :profile_image, :fill, 50, 50, fallback: "no-image-icon.jpg") %></td> <td><%= user.name %></td> <td><%= link_to 'Show', user_path(user), class: "user_#{user.id}" %></td> <td><p>フォロー数:</p></td> <td><p>フォロー数:</p></td> <td> <% if current_user != user %> <% if current_user.following?(user) %> <%= link_to 'フォローを外す', user_relationships_path(user), method: :delete %> <% else %> <%= link_to 'フォローする', user_relationships_path(user), method: :post %> <% end %> <% end %> </td> </tr> <% end %> </tbody> </table> ※フォロー数のところはこれから記述していく予定です

user.rb↓

class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :books has_many :book_comments, dependent: :destroy has_many :favorites, dependent: :destroy has_many :follower, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy#フォローしている has_many :followed, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy#フォローされている has_many :follower_user, through: :followed, source: :follower#フォローしている人 has_many :following_user, through: :follower, source: :followed#フォローされている人 #フォローする def follow(user_id) follower.create(followed_id: user_id) end #フォローを外す def unfollow(user_id) follower.find_by(followed_id: user_id).destroy end #既にフォローしているかの確認 def following?(user) following_user.include?(user) end attachment :profile_image, destroy: false validates :name, length: {maximum: 20, minimum: 2}, uniqueness: true validates :introduction, length: {maximum: 50} end

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

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

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

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

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

guest

回答1

0

自己解決

カラム名にあった記述ミスを修正したら解決しました

投稿2021/01/02 14:19

ooaai

総合スコア17

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問