rails 初心者です。
タイトルのエラーが表示されているのですが、以下に関係しそうなファイルを乗せるので、一緒に解決お願いいたします。
やったこと、
・ルーティングのresourcesは複数形になっている。
class FavoritesController < ApplicationController before_action :require_user_logged_in def create micropost = Favorite.find(params[:micropost_id]) current_user.like(micropost) flash[:success] = 'いいねをしました。' redirect_to user end
↓パーシャルとしてこのファイルの下で呼び出しています。
_like_button.html.erb <% if current_user.already_favorite?(micropost) %> <%= form_with(model: current_user.favorites.find_by(micropost_id: micropost.id), local: true, method: :delete) do |f| %> <%= hidden_field_tag :micropost_id, micropost.id %> <%= f.submit 'いい外す', class: 'btn btn-danger btn-block' %> <% end %> <% else %> <%= form_with(model: current_user.favorites.build,local: true) do |f| %> <%= hidden_field_tag :micropost_id, micropost.id %> <%= f.submit 'いいね', class: 'btn btn-primary btn-block' %> <% end %> <% end %>
#micropost.html.erb <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><%= 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/like_button', micropost: micropost %> </div> </div> </li> <% end %> <%= paginate microposts %> </ul>
_like_button.html.erbの already_favorite? メソッドはuser モデルに書いています。以下
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 :favorites, dependent: :destroy has_many :fav_microposts, through: :favorites, source: :micropost #いいね機能 def like(micropost) self.favorites.find_or_create_by(micropost_id :micropost.id) end def unlike(micropost) favorite = self.favorites.find_by(micropost_id: micropost.id) favorite.destroy if favorite end def already_favorite?(micropost) self.favorites.exists?(micropost.id) end end
Rails.application.routes.draw do root to: 'toppages#index' get 'login', to: 'sessions#new' post 'login', to: 'sessions#create' delete 'logout', to: 'sessions#destroy' get 'signup', to: 'users#new' resources :users, only: [:index, :show, :new, :create] do member do get :followings get :followers end end =begin resources :users, only: [:index, :show, :new, :create] do post :likes end =end resources :microposts, only: [:create, :destroy] resources :relationships, only: [:create, :destroy] resources :favorites , only: [:create, :destroy] end Prefix Verb URI Pattern Controller#Action root GET / toppages#index login GET /login(.:format) sessions#new POST /login(.:format) sessions#create logout DELETE /logout(.:format) sessions#destroy signup GET /signup(.:format) users#new followings_user GET /users/:id/followings(.:format) users#followings followers_user GET /users/:id/followers(.:format) users#followers users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new user GET /users/:id(.:format) users#show microposts POST /microposts(.:format) microposts#create micropost DELETE /microposts/:id(.:format) microposts#destroy relationships POST /relationships(.:format) relationships#create relationship DELETE /relationships/:id(.:format) relationships#destroy favorites POST /favorites(.:format) favorites#create favorite DELETE /favorites/:id(.:format) favorites#destroy
あなたの回答
tips
プレビュー