発生している問題・エラーメッセージ
エラーメッセージ ActionView::Template::Error (undefined method `content' for #<Favorite:0x00007f08d428cb28>): 10: </div> 11: 12: <div> 13: <p><%= favorite.content %></p> 14: </div> 15: </div> 16: </li> app/views/favorites/_favorites.html.erb:13:in `block in _app_views_favorites__favorites_html_erb__469108202852265684_69837948195740' app/views/favorites/_favorites.html.erb:2:in `_app_views_favorites__favorites_html_erb__469108202852265684_69837948195740' app/views/users/favorites.html.erb:24:in `_app_views_users_favorites_html_erb___552292666146660256_69837948317040'
###エラーが発生してるコード
views/favorites/_favorites.html.erb
ruby
1<div class="list-unstyled"> 2 <% favorites.each do |favorite| %> 3 <li class="media mb-3"> 4 <img class="mr-2 rounded" src="<%= gravatar_url(favorite.user, { size: 50 }) %>" alt=""> 5 <div class="media-body"> 6 <div> 7 <%= link_to favorite.user.name, user_path(favorite.user) %> <span class="text-muted"> 8 posted at <%= favorite.created_at %> 9 </span> 10 </div> 11 12 <div> 13 <p><%= favorite.content %></p> 14 </div> 15 </div> 16 </li> 17 <% end %> 18 <%= paginate favorites %> 19</div> 20 21
views/users/favorites.html.erb
ruby
1<div class="row"> 2 <aside class="col-sm-4"> 3 <div class="card"> 4 <div class="card-header"> 5 <h3 class="card-title"><%= @user.name %></h3> 6 </div> 7 <div class="card-body"> 8 <img class="rounded img-fluid" src="<%= gravatar_url(@user, { size: 500 }) %>" alt=""> 9 </div> 10 </div> 11 <%= render 'relationships/follow_button', user: @user %> 12 </aside> 13 <div class="col-sm-8"> 14 <ul class="nav nav-tabs nav-justified mb-3"> 15 <li class="nav-item"><a href="<%= user_path(@user) %>" class="nav-link <%= 'active' if current_page?(user_path(@user)) %>">Microposts 16 <span class="badge badge-secondary"><%= @count_microposts %></span></a></li> 17 <li class="nav-item"><a href="<%= followings_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followings_user_path(@user)) %>">Followings 18 <span class="badge badge-secondary"><%= @count_followings %></span></a></li> 19 <li class="nav-item"><a href="<%= followers_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(followers_user_path(@user)) %>">Followers 20 <span class="badge badge-secondary"><%= @count_followers %></span></a></li> 21 <li class="nav-item"><a href="<%= favorites_user_path(@user) %>" class="nav-link <%= 'active' if current_page?(favorites_user_path(@user)) %>">Favorites 22 <span class="badge badge-secondary"><%= @count_favorites %></span></a></li> 23 </ul> 24 <%= render 'favorites/favorites', favorites: @favorites %> 25 </div> 26</div> 27
views/favorites/_favorite_button.html.erb
ruby
1<% if current_user.likes?(micropost) %> 2 <%= form_with(model: current_user.favorites.find_by(micropost_id: micropost.id), local: true, method: :delete) do |f| %> 3 <%= hidden_field_tag :micropost_id, micropost.id %> 4 <%= f.submit 'Unfavorite', class: 'btn btn-primary d-inline' %> 5 <% end %> 6<% else %> 7 <%= form_with(model: current_user.favorites.build, local: true) do |f| %> 8 <%= hidden_field_tag :micropost_id, micropost.id %> 9 <%= f.submit 'Favorite', class: 'btn btn-primary d-inline' %> 10 <% end %> 11<% end %>
favorites_controller.rb
ruby
1class FavoritesController < ApplicationController 2 before_action :require_user_logged_in 3 4 def create 5 micropost = Micropost.find(params[:micropost_id]) 6 current_user.favorite(micropost) 7 flash[:success] = 'お気に入りに追加しました。' 8 redirect_to root_url 9 end 10 11 def destroy 12 micropost = Micropost.find(params[:micropost_id]) 13 current_user.unfavorite(micropost) 14 flash[:success] = 'お気に入りから削除しました。' 15 redirect_to root_url 16 end 17end 18
users/controller.rb
ruby
1class UsersController < ApplicationController 2 before_action :require_user_logged_in, only: [:index, :show, :followings, :followers, :favorites] 3 def index 4 @users = User.order(id: :desc).page(params[:page]).per(25) 5 end 6 7 def show 8 @user = User.find(params[:id]) 9 @microposts = @user.microposts.order(id: :desc).page(params[:page]) 10 counts(@user) 11 12 @user = User.find(params[:id]) 13 @favorites = @user.favorites.order(id: :desc).page(params[:page]) 14 counts(@user) 15 end 16 17 def new 18 @user = User.new 19 end 20 21 def create 22 @user = User.new(user_params) 23 24 if @user.save 25 flash[:success] = 'ユーザを登録しました。' 26 redirect_to @user 27 else 28 flash.now[:danger] = 'ユーザの登録に失敗しました。' 29 render :new 30 end 31 end 32 33 def followings 34 @user = User.find(params[:id]) 35 @followings = @user.followings.page(params[:page]) 36 counts(@user) 37 end 38 39 def followers 40 @user = User.find(params[:id]) 41 @followers = @user.followers.page(params[:page]) 42 counts(@user) 43 end 44 45 def favorites 46 @user = User.find(params[:id]) 47 @favorites = @user.favorites.order(id: :desc).page(params[:page]) 48 counts(@user) 49 50 end 51 private 52 53 def user_params 54 params.require(:user).permit(:name, :email, :password, :password_confirmation) 55 end 56end 57
###補足
かれこれこの問題で1週間は考えていますが、解決できません。
足りないソースコードがあれば載せます。アドバイスよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/03 05:57