link_to でPOSTに遷移させたい。
Q&A
解決したいこと
link_to でPOSTに遷移させたい。
今はbutton_toで解決しましたがlink_toで遷移させたい。
Railsでインスタグラムに似たようなWebアプリをつくっています。記事をいいねする機能の実装中にエラーが発生しました。
解決方法を教えて下さい。
発生している問題・エラー
ActionController::RoutingError
No route matches [GET]
該当するソースコード
<section class="hero is-success">
<div class="hero-body">
<div class="container">
<h1 class="title">
レシピ一覧
</h1>
</div>
</div>
</section>
<section class="section">
<div class="container">
<div class="columns is-multiline">
<% @recipes.each do |recipe| %>
<div class="column is-4">
<div class="card">
<div class="card-image">
<figure class="image is-4by3">
<%= link_to recipe_path(recipe) do %>
<%= attachment_image_tag recipe, :image %>
<% end %>
</figure>
</div>
<div class="card-content">
<div class="media">
<div class="media-left">
<figure class="image is-48x48">
<%= link_to user_path(recipe.user.id) do %>
<%= attachment_image_tag recipe.user, :profile_image, fallback: "no-image.png" %>
<% end %>
</figure>
</div>
<div class="media-content">
<div class="title"><%=link_to recipe.user.username, user_path(recipe.user) %></div>
<% if current_user.already_favorited?(recipe) %>
<%= button_to recipe_favorites_path(recipe), method: :delete do %>
<i class="fas fa-heart"></i>
<%= recipe.favorites.count %>
<% end %>
<% else %>
<%= button_to recipe_favorites_path(recipe), method: :post do %>
<i class="far fa-heart"></i>
<%= recipe.favorites.count %>
<% end %>
<% end %>
</div>
</div>
<div class="content">
<time><%= recipe.updated_at.strftime("%Y-%m-%d %H:%M") %></time>更新
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
</section>
Rails.application.routes.draw do
devise_for :users
root to: "home#index"
resources :users
resources :recipes do
resource :favorites, only: [:create, :destroy]
end
end
class FavoritesController < ApplicationController
def create
@favorite = current_user.favorites.create(recipe_id: params[:recipe_id])
redirect_back(fallback_location: root_path)
#詳細画面と一覧画面でいいねを押したときに1つ前の画面に戻る
end
def destroy
@recipe = Recipe.find(params[:recipe_id])
@favorite = current_user.favorites.find_by(recipe_id: @recipe.id)
@favorite.destroy
redirect_back(fallback_location: root_path)#URLが見つからな買ったらルートパスにいく
end
Rails.application.routes.draw do
devise_for :users
root to: "home#index"
resources :users
resources :recipes do
resource :favorites, only: [:create, :destroy]
end
end
Prefix Verb URI Pattern Controller#Action
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session GET /users/sign_out(.:format) devise/sessions#destroy
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
user_password PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
POST /users/password(.:format) devise/passwords#create
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
user_registration PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
POST /users(.:format) devise/registrations#create
root GET / home#index
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
recipe_favorites DELETE /recipes/:recipe_id/favorites(.:format) favorites#destroy
POST /recipes/:recipe_id/favorites(.:format) favorites#create
recipes GET /recipes(.:format) recipes#index
POST /recipes(.:format) recipes#create
new_recipe GET /recipes/new(.:format) recipes#new
edit_recipe GET /recipes/:id/edit(.:format) recipes#edit
recipe GET /recipes/:id(.:format) recipes#show
PATCH /recipes/:id(.:format) recipes#update
PUT /recipes/:id(.:format) recipes#update
DELETE /recipes/:id(.:format) recipes#destroy
refile_app /attachments #<Refile::App app_file="/Users/mino/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/bundler/gems/refile-46b4178654e6/lib/refile/app.rb">
rails_postmark_inbound_emails POST /rails/action_mailbox/postmark/inbound_emails(.:format) action_mailbox/ingresses/postmark/inbound_emails#create
rails_relay_inbound_emails POST /rails/action_mailbox/relay/inbound_emails(.:format) action_mailbox/ingresses/relay/inbound_emails#create
rails_sendgrid_inbound_emails POST /rails/action_mailbox/sendgrid/inbound_emails(.:format) action_mailbox/ingresses/sendgrid/inbound_emails#create
rails_mandrill_inbound_health_check GET /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#health_check
rails_mandrill_inbound_emails POST /rails/action_mailbox/mandrill/inbound_emails(.:format) action_mailbox/ingresses/mandrill/inbound_emails#create
rails_mailgun_inbound_emails POST /rails/action_mailbox/mailgun/inbound_emails/mime(.:format) action_mailbox/ingresses/mailgun/inbound_emails#create
rails_conductor_inbound_emails GET /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#index
POST /rails/conductor/action_mailbox/inbound_emails(.:format) rails/conductor/action_mailbox/inbound_emails#create
new_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/new(.:format) rails/conductor/action_mailbox/inbound_emails#new
edit_rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id/edit(.:format) rails/conductor/action_mailbox/inbound_emails#edit
rails_conductor_inbound_email GET /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#show
PATCH /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
PUT /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#update
DELETE /rails/conductor/action_mailbox/inbound_emails/:id(.:format) rails/conductor/action_mailbox/inbound_emails#destroy
new_rails_conductor_inbound_email_source GET /rails/conductor/action_mailbox/inbound_emails/sources/new(.:format) rails/conductor/action_mailbox/inbound_emails/sources#new
rails_conductor_inbound_email_sources POST /rails/conductor/action_mailbox/inbound_emails/sources(.:format) rails/conductor/action_mailbox/inbound_emails/sources#create
rails_conductor_inbound_email_reroute POST /rails/conductor/action_mailbox/:inbound_email_id/reroute(.:format) rails/conductor/action_mailbox/reroutes#create
rails_service_blob GET /rails/active_storage/blobs/redirect/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_service_blob_proxy GET /rails/active_storage/blobs/proxy/:signed_id/*filename(.:format) active_storage/blobs/proxy#show
GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs/redirect#show
rails_blob_representation GET /rails/active_storage/representations/redirect/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_blob_representation_proxy GET /rails/active_storage/representations/proxy/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/proxy#show
GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations/redirect#show
rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require('jquery')
//= require rails-ujs
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require bootstrap.min
//= require_tree .
//= require bootstrap
//= require activestorage
Rails.start()
Turbolinks.start()
ActiveStorage.start()
自分で試したこと
views.rbのbutton_toのところは元々link_toでした。
button_toにするとエラーなく処理が進むのでbutton_toにしています。
質問は初投稿なので何の情報を記載するのか分からずに質問を投稿しています。
不足している情報があればご教示ください。