【前提・実現したいこと】
フォロー機能を確認したい。
【発生している問題・エラーメッセージ】
ルーティングエラーが出ています。
【該当のソースコード】
フォローボタン
RubyOnRails
1 <%= link_to user_relationships_path(@user), method: :post do%> 2 <button class="btn btn-primary">フォロー</button> 3 <% end %>
ルーティング
RubyOnRails
1 resources :users do 2 resource :relationships, only: [:create, :destroy] 3 get :followings, on: :member 4 get :followers, on: :member 5 end
relationships_controller.rb
RubyOnRails
1class RelationshipsController < ApplicationController 2 before_action :authenticate_user! 3 def create 4 following = current_user.relationships.build(follower_id: params[:user_id]) 5 following.save 6 redirect_to request.referrer || root_path 7 end 8 9 def destroy 10 following = current_user.relationships.find_by(follower_id: params[:user_id]) 11 following.destroy 12 redirect_to request.referrer || root_path 13 end 14end
【自分で調べたことや試したこと】
想定としてはフォローができるつまりインサート文でリレーションシップステーブルにデータが入ることを想定していますが、上記エラーのためうまくいきません。
ファイルがないからかとviewのrelationshipsフォルダにファイルを作りましたがうまくいきませんでした。
ちなみに参考にした動画は以下の動画です。11分のところです。
私が実行した場合はエラーとなりました。
https://www.youtube.com/watch?v=iAIRNEsA-7g
【使っているツールのバージョンなど補足情報】
ruby 3.1.2
rails 6.1.6
以上です。
おそらくrailsの仕組みを理解していないので解消できないと考えております。
不足の情報がありましたらお知らせください。
何卒よろしくお願いいたします。
恐らくrails6で発生しているpostが自動でgetになることによるものだということまでは判明しました。しかし、その後の解消方法を元に動かしても解消されない現状です。
参考
rails6でPostがGETと認識されてしまう
https://teratail.com/questions/282169
【備忘録】【Rails6】「 <%= link_to({method: "post"})%>」がGETになる問題
https://blog.ezic.info/43631.html
回答1件
あなたの回答
tips
プレビュー