エラーが取れないです。No route matches [PATCH] "/articles/1/comments"
記事についたコメントを編集出来るようにしたいのです。
コメントをformに持っていき編集して更新ボタンをクリックすると
No route matches [PATCH] "/articles/1/comments"
のようなエラーが出てきてrotesなどを色々変えて見たのですが全然、治らないので見ていただけないでしょうか?
_form.html.erb
<%= form_for(comment, url: article_comments_path) do |f| %>
<% if comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(comment.errors.count, "error") %></h2>
<ul>
<% comment.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.label :article_id %>
<%= f.number_field :article_id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
routes.rb
rails.application.routes.draw do
get '/users' => 'users#index'
get '/users/:id' => 'users#show'
devise_for :users
resources :articles do
resources :comments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
comments_controller.rb
class CommentsController < ApplicationController
before_action :set_comment, only: [:show, :edit, :update, :destroy]
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
end
# GET /comments/1
# GET /comments/1.json
def show
end
# GET /comments/new
def new
@comment = Comment.new
end
# GET /comments/1/edit
def edit
end
# POST /comments
# POST /comments.json
def create
@article = Article.find(params[:article_id])
@comment = Comment.new(comment_params)
redirect_to article_path(@article)
respond_to do |format|
if @comment.save
format.html { redirect_to @article, notice: 'Comment was successfully created.'}
format.json { render :show, status: :created, location: @article } and return
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /comments/1
# PATCH/PUT /comments/1.json
def update
respond_to do |format|
if @comment.update(comment_params)
format.html { redirect_to @comment, notice: 'Comment was successfully updated.' }
format.json { render :show, status: :ok, location: @comment }
else
format.html { render :edit }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /comments/1
# DELETE /comments/1.json
def destroy
@comment.destroy
respond_to do |format|
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:content, :article_id)
end
end