解決したいこと
プログラミングを始めて2ヶ月の初学者です。
現在、Ruby on Railsで画像投稿ができるアプリで
非同期通信のコメントを作成しています。
その中でJavaScriptのビューを反映することができず苦戦しております。
自分なりに調べたり、考えたのですが解決することが難しかったため、
どなたかお力添えいただけますと嬉しいです。
現状
こちらの記事を参考に実装していたのですが、
https://qiita.com/nakachan1994/items/a7d0957afa9dfd9146ed
↓ コメントを送信すると、以下のようにjsファイルで指定した文字列が出てきてしまいました。
こちらを非同期で指定したビューにするにはどのように記述を変えるべきなのか教えていただきたいです。
(ブラウザの検証ツールではエラーは表示されていない状況でした)
自分で試したこと
①部分テンプレートに切り出す
投稿の詳細ページにコメント機能を実装しているので、こちらの2つの部分(コメントフォーム、コメント一覧表示)を切り出しました。
app/views/items/show.html.erb
<div class="comments"> <div class="comments__contents"> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <% if user_signed_in? %> <div class="new-comment"> <%= render 'comments/form', item: @item, comment: @comment %> </div> <% end %> <%# // ログインしているユーザーには上記を表示する %> <div class="comment-header"> 画像を入れる </div> <ul class="comments_lists"> <div id="comments_area"> <%= render 'comments/item_comment', item: @item, comment: @comment %> </div> </ul> </div> </div>
コメントフォーム
app/views/comments/_form.html.erb
<%= form_with model: [@item,@comment] do |f| %> <%= render 'shared/error_messages', model: f.object %> <div class="comment__field"> <%= f.text_field :text, class: :post__comment, placeholder: 'コメントを入力する' %> <br> <%= f.submit "コメントする", class: :comment__btn %> </div> <% end %>
コメント一覧表示
app/views/comments/_item_comment.html.erb
<% if @item.comments.present? %> <% @comments.each do |comment| %> <li class="comments_list"> <div class="comment__profile"> <div class="comment_img"> <% if comment.user.image?%> <%= image_tag comment.user.image.url , class: :comment_img %> <% else %> <%= image_tag "/assets/default.png", class: :comment_img %> <% end %> <%= link_to comment.user.name, user_path(comment.user.id), class: :comment_user %> </div> <div class="comment_text"> <%= comment.text %> </div> </div> <% if user_signed_in? && ( current_user.id == @item.user_id || current_user.id == comment.user.id ) %> <%= link_to "削除する", item_comment_path(comment.item.id, comment.id), method: :delete ,class: :destroy__btn, remote: true %> <% end %> </li> <% end %> <% else %> <div class="comment__none"> コメントはありません </div> <% end %>
②対応するjsファイルをつくる
ビューのコメント内に作成しています。
app/views/comments/item_comments.js
// id="comments_areaにをrenderで指定した部分テンプレートに更新 $("#comments_area").html("<%= j(render 'comments/item_comment', item: @item, comment: @comment) %>"); // フォームを送信したあとにフォームの値を空にする $("textarea").val('');
・・ 結果的にこちらの文字列( j(render 'comments/item_comment', item: @item, comment: @comment) )が呼ばれてしまいます。
③コントローラーの設定
コメントが正常に行われた場合、②で指定したjsのファイルを呼び出しています。
app/controllers/comment_controller.rb
def create @item = Item.find(params[:item_id]) @comment = Comment.new(comment_params) if @comment.save render :item_comments else @comments = @item.comments.includes(:user) render 'items/show' end end
考えられること
・コントローラーではJavaScriptのファイルが呼び出されていることから、コントローラーの記述は合っているのではないか。
・部分テンプレートで切り出したものは、送信しない場合正しく表示されているため、渡しているインスタンス変数は合っているのではないか。
→JavaScriptが適応されないということは、ファイルが指定されていないのではないか?と考えました。
追記
上記にファイルを指定したのですが、表示結果は変わらずの状況でした。
require("@rails/ujs").start() // require("turbolinks").start() require("@rails/activestorage").start() require("channels") require("../nav") require('jquery') require('../../../app/views/comments/item_comments') #こちらを追記
お手数ですが、ご教授いただけますと幸いです。
※以下でも質問しております。
(リンクタグに反映されなかったため、以下の表記で失礼いたします)
https://ja.stackoverflow.com/questions/89055/%e9%9d%9e%e5%90%8c%e6%9c%9f%e9%80%9a%e4%bf%a1%e3%81%ae%e3%82%b3%e3%83%a1%e3%83%b3%e3%83%88%e3%82%92%e8%a1%a8%e7%a4%ba%e3%81%99%e3%82%8b%e3%81%ab%e3%81%af%e3%81%a9%e3%81%ae%e3%82%88%e3%81%86%e3%81%ab%e3%81%99%e3%82%8c%e3%81%b0%e8%89%af%e3%81%84%e3%81%ae%e3%81%8b
まだ回答がついていません
会員登録して回答してみよう