下記railsチュートリアルにてフォローボタンを付与しました。
https://railstutorial.jp/chapters/following_users?version=5.1#cha-following_users
特にそれだけでは問題なく開発できたのですが、フォロー解除した際にモーダルウィンドウにて再度本当に解除するか確認をとりたいと思っています。
_form.html.erb
そのため、下記URLを参考に実装したのですが、うまくいきませんでした。
https://qiita.com/niwaken/items/ffbce52fb024fd369f24
原因としては、_form.html.erbに下記のようなform_forヘルパーメソッドを付与することによって、
<div class="modal-content"> <%= form_for(current_user.active_relationships.find_by(followed_id: @user.id), html: { method: :delete }, remote: true) do |f| %> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Mind</h5> <!-- <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> --> <!-- </button> --> </div> <div class="modal-body"> <button type="button" id="btn1" class="btn btn-secondary">フォローをやめる</button> </div> <div class="modal-footer1"> <button type="button" id="btn2" class="btn btn-secondary" data-dismiss="modal">キャンセル</button> <!-- <button type="button" class="btn btn-primary">Save changes</button> --> </div> </div> </div> コード
destroy.js.erbに行って欲しいのですが、ターミナルをみると再度new.js.erbに飛ばされているようです。
Started
1Processing by RelationshipsController#new as JS 2 User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1 3 Rendering relationships/new.js.erb 4 Rendered relationships/_form.html.erb (0.4ms) 5 Rendered users/_follow.html.erb (18.0ms) 6 Rendered relationships/new.js.erb (44.4ms) 7Completed 500 Internal Server Error in 102ms (ActiveRecord: 0.3ms) 8コード
また、こうするとモーダルもうまく表示されません。
※form_for文を削除するとviewにモーダルは表示されます。
何か、解決方法をご存知の方がいらっしゃいましたらアドバイス等を頂ければと思います。
あなたの回答
tips
プレビュー