Ruby on Rails4_2_3
ローカル環境
プログラミングを勉強中でアプリケーションにAjaxを実装。
処理を行うと画面遷移はせずにアプリケーションは動かず、エラー表示もない。
ログを辿ると以下のようなエラーになっていました。
サーバーログ
Started DELETE "/blogs/3/comments/25" for ::1 at 2017-02-13 20:27:57 +0900 Processing by CommentsController#destroy as JS Parameters: {"blog_id"=>"3", "id"=>"25"} Comment Load (0.5ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 25]] (0.2ms) BEGIN SQL (0.7ms) DELETE FROM "comments" WHERE "comments"."id" = $1 [["id", 25]] (0.4ms) COMMIT Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.8ms) ArgumentError - too few arguments: app/controllers/comments_controller.rb:26:in `destroy'
記述したコード類は以下になります。
CommentsController
class CommentsController < ApplicationController 省略 def destroy @comment = Comment.find(params[:id]) @comment.destroy format.html { redirect_to blog_path(@blog) } format.js { render :index } end 省略
view
省略 <ul> <% comments.each do |comment| %> <% unless comment.id.nil? %> <li> <p class="left"><%= comment.user.name %>さんがコメントしました。</p> <p class="left"><%= comment.content %></p> <% if current_user.id == comment.user.id %> <p class="right"> <%= link_to '', blog_comment_path(blog, comment), class: "fa fa-trash-o fa-lg", method: :delete, remote: true, data: { confirm: '本当に削除していいですか?' } %> </p> <% end %> </li> <% end %> <% end %> </ul> <%= form_for([blog, comment], remote: true) do |f| %> <%= f.hidden_field :blog_id %> <div class="field"> <%= f.text_field :content, placeholder: "内容", class: "form-control" %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> 省略
index.js.erb
$("#comments_area").html("<%= j(render 'comments/index', { comments: @comment.blog.comments, blog: @comment.blog }) %>") $(':text').val('')
createアクションは正常に動作するんですが、destroyアクションがエラーになります。
何か不足している部分があればアドバイスしていただけると助かります。
回答1件
あなたの回答
tips
プレビュー