前提
rails で投稿アプリを作っています。
以前までは、いいねやコメントの削除はできていましたが、
Routing Error
No route matches [GET] "/posts/1/likes/1"
このようなエラーが出るようになりました。
サイトなどで調べたのを基に、
javascriptについて調べていじっていたのが原因かなと考えています。
発生している問題・エラーメッセージ
Routing Error No route matches [GET] "/posts/1/likes/1"
該当のソースコード
routes.rb resources :posts do resources :likes, only: [:create, :destroy] resources :comments, only: [:create, :destroy] end
apprication.js import Rails from "@rails/ujs" import Turbolinks from "turbolinks" import * as ActiveStorage from "@rails/activestorage" import "channels" import "script.js" import 'jquery' import 'jquery_ujs' import "bootstrap"; import "../stylesheets/application"; Rails.start() Turbolinks.start() ActiveStorage.start()
apprication.html <!DOCTYPE html> <html> <head> <title>Test</title> <meta name="viewport" content="width=device-width,initial-scale=1"> <%= csrf_meta_tags %> <%= csp_meta_tag %> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.14.0/css/all.css"> <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %> </head>
webpack/enviroment.js const { environment } = require('@rails/webpacker') const webpack = require('webpack') environment.plugins.prepend('Provide', new webpack.ProvidePlugin({ $: 'jquery/src/jquery', jQuery: 'jquery/src/jquery' }) ) module.exports = environment
Gemfile gem 'devise' gem 'ancestry' gem 'roo' gem 'jquery-rails'
show.html <div id="like_btn_<%= @post.id %>"> <%= render 'likes/like', post: @post %> </div> <% if user_signed_in? && current_user.id == @post.user_id %> <%= link_to '編集する', edit_post_path(@post.id) %><br> <%= link_to '削除する', post_path(@post.id),method: :delete, "data-confirm"=>"本当に消しますか?" %><br> <% end %> <%= link_to '投稿一覧ページへ', posts_path %> <div class="comment-wrapper"> <p>コメント件数:<%= @comments.count %></p> <% if user_signed_in? %> <%= form_with(model: [@post, @comment], local: true) do |f| %> <%= f.text_area :content %> <%= button_tag type: "submit" do %> <i class="far fa-comments"></i> コメントする <% end %> <% end %> <% end %> <% @comments.each do |comment| %> <div class="comments-index mb-2"> <%= comment.user.name %> <%= comment.created_at.strftime("%Y-%m-%d %H:%M") %><br> <%= comment.content %> <div class="d-flex"> <% if comment.user == current_user %> <div class="comment-delete"> <%= link_to "削除", post_comment_path(comment.post, comment), method: :delete, class: "btn-sm btn-danger" %> </div> </div> </div> <% end %> <% end %> </div>
_like.html <% if user_signed_in? %> <% if current_user.already_liked?(post) %> <%= link_to post_like_path(post), method: :delete, remote: true do %> ♥<%= @post.likes.count %> いいね <% end %> <% else %> <%= link_to post_likes_path(post), method: :post, remote: true do %> ♡<%= @post.likes.count %> いいね <% end %> <% end %> <% else %> <i class="fas fa-heart"></i><%= post.likes.count %> <% end %>
補足情報(FW/ツールのバージョンなど)
なにをどうしていいのかわからない状態であり、
なにが導入されていて導入されていないのか訳が分からない状態になってしまっています。
消したほうがいい記述などがあれば教えていただきたいです。
回答1件
あなたの回答
tips
プレビュー