初心者です。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>
お願いいたします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/31 15:19