LoginSignup
oiken
@oiken (KENTARO OIKAWA)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Ruby on Rails いいね機能 Routing Errorの解決が出来ません

Q&AClosed

Routing Error

Ruby on Rails でいいね機能を実装する為に記述しましたがいいねを外す際にどうしてもRouting Errorが出てしまいます。解決方法を教えて頂きたいです。

発生している問題・エラー

Routing Error
No route matches [DELETE] "/tweets/4/favorites"

ターミナル

ターミナル.png

rails routes

rails routes.png

または、問題・エラーが起きている画像をここにドラッグアンドドロップ

tweets index

<%= form_with(url: search_tweets_path, local: true, method: :get, class: "search-form") do |form| %>
  <%= form.text_field :keyword, placeholder: "投稿を検索する", class: "search-input" %>
  <%= form.submit "検索", class: "search-btn" %>
<% end %>
<div class="contents row">
  <% @tweets.each do |tweet| %>
    <%= render partial: "tweet", locals: { tweet: tweet } %>
     <tr>
       <td>
        <% if current_user %>
            <% if current_user.already_favorited? (tweet) %>
              <%= link_to  tweet_favorites_path(tweet), method: :delete  do%>
              <i class="fas fa-heart"></i>
            <% end %>
               <%= tweet.favorites.count %>
             <% else %>
               <%= link_to tweet_favorites_path(tweet), method: :post do %>
               <i class="far fa-heart"></i>
             <% end %>
               <%= tweet.favorites.count %>
             <% end %>     
        </td>
      </tr>
    <%end%>
  <% end %>
</div>

routes

Rails.application.routes.draw do
  devise_for :users, controllers: {
   omniauth_callbacks: 'users/omniauth_callbacks',
   registrations: 'users/registrations'
 }
  root to: 'tweets#index'
  resources :tweets do
    resources :favorites, only: [:create, :destroy]
  end

    resources :tweets do
    resources :comments, only: :create
    collection do
      get 'search'
    end
  end
  resources :users
end

tweets_controller

class TweetsController < ApplicationController
  before_action :authenticate_user!, except: [:index]
  before_action :set_tweet, only: [:edit, :show]
  before_action :authenticate_user!, only: [:create]



  def index
    @tweets = Tweet.includes(:user).order("created_at DESC")
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
    redirect_to root_path
  end

  def edit
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
    @comment = Comment.new
    @comments = @tweet.comments.includes(:user)
  end

  def search
    @tweets = Tweet.search(params[:keyword])
  end

 private
  def tweet_params
    params.require(:tweet).permit(:title, :iamge, :text).merge(user_id: current_user.id)
  end

  def set_tweet
    @tweet = Tweet.find(params[:id])
  end
end


favorites_controller

class TweetsController < ApplicationController
  before_action :authenticate_user!, except: [:index]
  before_action :set_tweet, only: [:edit, :show]
  before_action :authenticate_user!, only: [:create]



  def index
    @tweets = Tweet.includes(:user).order("created_at DESC")
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
    redirect_to root_path
  end

  def edit
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
    @comment = Comment.new
    @comments = @tweet.comments.includes(:user)
  end

  def search
    @tweets = Tweet.search(params[:keyword])
  end

 private
  def tweet_params
    params.require(:tweet).permit(:title, :iamge, :text).merge(user_id: current_user.id)
  end

  def set_tweet
    @tweet = Tweet.find(params[:id])
  end
end


自分で試したこと

routes でtweet_favorite_pathと記載があった為下記に変更しましたがエラー(ActionController::UrlGenerationError)が出てしまいました。

tweets/index
     <%= link_to tweet_favorite_path(tweet), method: :delete do%>

0

No Answers yet.

Your answer might help someone💌