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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

501閲覧

ユーザーがいいねを押した投稿を一覧にしてに表示したい。

asato2000

総合スコア22

Ruby on Rails 5

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

Ruby on Rails

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

0グッド

0クリップ

投稿2020/08/28 16:53

初心者です。railsでツイッターのようなものを作っています。そしていいねした投稿をusers/fav_posts.heml.erbに表示したいと考えています。そこでfav_postsなどを定義して進めていくとエラーが出てしまいどこがおかしいのかわからないようのなってしまいました。このエラーの箇所がおかしいのかfav_postsメソッドがもともとおかしいのか、、

発生している問題・エラーメッセージ

エラーメッセージ [リンク内容](https://gyazo.com/7ec2b8751ea873ef4002a3b6f9309427)

user.rb (model)

class User < ApplicationRecord before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 50 } validates :email, presence: true, length: { maximum: 255 }, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i }, uniqueness: { case_sensitive: false } has_secure_password has_many :microposts has_many :relationships has_many :followings, through: :relationships, source: :follow has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id' has_many :followers, through: :reverses_of_relationship, source: :user has_many :favorites has_many :fav_posts, through: :favorites, source: :micropost def follow(other_user) unless self == other_user self.relationships.find_or_create_by(follow_id: other_user.id) end end def unfollow(other_user) relationship = self.relationships.find_by(follow_id: other_user.id) relationship.destroy if relationship end def following?(other_user) self.followings.include?(other_user) end def feed_microposts Micropost.where(user_id: self.following_ids + [self.id]) end def favorite(micropost) self.favorites.find_or_create_by(micropost_id: micropost.id) end def unfavorite(micropost) favorite = self.favorites.find_by(micropost_id: micropost.id) favorite.destroy if favorite end def fav_post?(micropost) self.fav_posts.include?(micropost) end def feed_fav_posts Micropost.where(user_id: self.fav_post_ids ) end end

users_controller.rb

class UsersController < ApplicationController before_action :require_user_logged_in, only: [:index, :show, :followings, :followers] def index @users = User.order(id: :desc).page(params[:page]).per(25) end def show @user = User.find(params[:id]) @microposts = @user.microposts.order(id: :desc).page(params[:page]) counts(@user) end def new @user = User.new end def create @user = User.new(user_params) if @user.save flash[:success] = "ユーザーを登録しました。" redirect_to @user else flash.now[:danger] = "ユーザーの登録に失敗しました。" render:new end end def followings @user = User.find(params[:id]) @followings = @user.followings.page(params[:page]) counts(@user) end def followers @user = User.find(params[:id]) @followers = @user.followers.page(params[:page]) counts(@user) end def fav_posts @user = User.find(params[:id]) @fav_posts = @user.fav_posts.page(params[:page]) end private def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation) end end

fav_posts.html.erb

<div class="row"> <aside class="col-sm-4"> <div class="card"> <div class="card-header"> <h3 class="card-title"><%=@user.name%></h3> </div> <div class="card-body"> <img class="rounded img-fluid" src="<%= gravatar_url(@user,{size:500})%>" alt=""> </div> </div> </aside> <div class="col-sm-8"> <ul class="nav nav-tabs nav-justified mb-3"> <li class="nav-item"><a href="<%= user_path(@user)%>" class="nav-link <%= current_page?(user_path(@user))%>">Microposts <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%="active" if current_page?(followings_user_path(@user))%>">Followings <span class="badge badge-secondary"><%= @count_followings %></span></a></li> <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers <span class="badge badge-secondary"><%= @count_followers %></span></a></li> <li class="nav-item"><a href="<%= fav_posts_user_path(@user) %>" class="nav-link <%= "active" if current_page?(fav_posts_user_path(@user))%>">Favorites <span class="badge badge-secondary"><%= @count_favorites %></span></a></li> </ul> <%= render "micropposts/microposts", microposts: @fav_posts %> </div> </div>

_microposts/microposts

<ul class="list-unstyled"> <% microposts.each do |micropost| %> <li class="media mb-3"> <img class="mr-2 rounded" src=" <%= gravatar_url(micropost.user, { size: 50 }) %> " alt=""> <div class="media-body"> <div> <%= link_to micropost.user.name, user_path(micropost.user) %> <span class="text-muted">posted at <%= micropost.created_at %> </span> </div> <div> <p class="mb-0"> <%= micropost.content %> </p> </div> <div> <% if current_user == micropost.user %> <%= link_to "Delete", micropost, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-danger btn-sm' %> <% end %> <%=render "favorites/favorite_button", micropost: micropost %> </div> </div> </li> <% end %> <%= paginate microposts %> </ul>

お願いいたします!

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーメッセージは画像ではなくtextで貼ってください。

で、fle名、pathが違います、_microposts/microposts でなく microposts/microposts.html.erb です。

投稿2020/08/29 00:01

winterboum

総合スコア23333

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

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

asato2000

2020/08/31 15:19

ありがとうございます!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問