Lilspree
Lilspree

Reputation: 135

Another Rails No route matche [Patch]

I am trying to edit an article and re-save it. I can create a new article and save that. I can even comment on the article and save that. When I go to edit the article it auto populates the form with the text to be edited but when I try to save that it gives me the "No route matches [Patch]"/artices.7" error.

article controller

class ArticlesController < ApplicationController

def index
    @articles = Article.all
end

def show
    @articles = Article.find(params[:id])
end

def new
    @articles = Article.new
end

def edit
    @articles = Article.find(params[:id])
end

def create
    @articles = Article.new(articles_params)

    if @articles.save
        redirect_to @articles
    else
        render 'new'
    end
end

def update
    @articles = Article.find(params[:id])

    if @articles.update(articles_params)
        redirect_to @articles
    else
        render 'edit'
    end
end

def destroy
    @articles = Article.find(params[:id])
    @articles.destroy

    redirect_to articles_path
end

private
    def articles_params
        params.require(:articles).permit(:title, :text)
    end
end

comment controller

class CommentsController < ApplicationController

def create
  @articles = Article.find(params[:article_id])
  @comment = @articles.comments.create(comment_params)
  redirect_to articles_path(@articles)
end

def destroy
  @articles = Article.find(params[:article_id])
  @comment = @articles.comments.find(params[:id])
  @comment.destroy
  redirect_to articles_path(@articles)
end

private
  def comment_params
    params.require(:comment).permit(:commenter, :body)
  end
end

routes

Rails.application.routes.draw do

  resources  :articles do
  resources :comments
 end
end

When I run the edit blog edit.html.erb it gives me "No route matches [Patch] error. Should I do my routes a different way? I thought resources would cover that. Here is the edit file

   <h1>Editing article</h1>

<%= form_for :articles, url: articles_path(@articles), method: :patch do  |f| %>

  <% if @articles.errors.any? %>
    <div id="error_explanation">
      <h2>
        <%= pluralize(@articles.errors.count, "error") %> prohibited
        this article from being saved:
      </h2>
      <ul>
        <% @articles.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p>
    <%= f.label :title %><br>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= f.label :text %><br>
   <%= f.text_area :text %>
  </p>

  <p>
    <%= f.submit %>
  </p>

<% end %>

Updated with rake routes

Prefix Verb   URI Pattern                                       Controller#Action
  nba GET    /nba(.:format)                   nba#games
  nba_index GET    /nba(.:format)                                    nba#index
   POST   /nba(.:format)                      nba#create
   new_nba GET    /nba/new(.:format)                                nba#new
            edit_nba GET    /nba/:id/edit(.:format)                                   nba#edit
                     GET    /nba/:id(.:format)                                nba#show
                     PATCH  /nba/:id(.:format)                                nba#update
                     PUT    /nba/:id(.:format)                                nba#update
                     DELETE /nba/:id(.:format)                                nba#destroy
    article_comments GET    /articles/:article_id/comments(.:format)          comments#index
                     POST   /articles/:article_id/comments(.:format)          comments#create
 new_article_comment GET    /articles/:article_id/comments/new(.:format)      comments#new
edit_article_comment GET    /articles/:article_id/comments/:id/edit(.:format) comments#edit
     article_comment GET    /articles/:article_id/comments/:id(.:format)      comments#show
                     PATCH  /articles/:article_id/comments/:id(.:format)      comments#update
                     PUT    /articles/:article_id/comments/:id(.:format)      comments#update
                     DELETE /articles/:article_id/comments/:id(.:format)      comments#destroy
            articles GET    /articles(.:format)                               articles#index
                     POST   /articles(.:format)                               articles#create
         new_article GET    /articles/new(.:format)                           articles#new
        edit_article GET    /articles/:id/edit(.:format)                      articles#edit
             article GET    /articles/:id(.:format)                           articles#show
                     PATCH  /articles/:id(.:format)                           articles#update
                     PUT    /articles/:id(.:format)                           articles#update
                     DELETE /articles/:id(.:format)                           articles#destroy
                root GET    /                                                 welcome#index

Upvotes: 1

Views: 147

Answers (1)

Topher Hunt
Topher Hunt

Reputation: 4804

I think I found it, in your form_for declaration you specified articles_path rather than article_path. Those are two different methods and they expect different parameters. Use article_path instead and you should get the expected result.

Also, this doesn't make any difference in terms of the computer processing, but you should name your instance variable @article instead of @articles, because it only refers to one object, rather than a list of objects. As you work with Rails (especially the routes, controller names, model names etc.) you'll keep noticing that the framework is very picky about when you use singulars and when you use plurals, which is of course why you made the mistake of using articles_path as explained above.

Rails makes it even more frustrating because they didn't inform you that you were using the wrong method. articles_path doesn't require an article ID, so ideally Rails would raise an error when you gave it one, but it turns out that all the _path and _url route helper methods accept an additional variable so you can define the file extension of the URL (which you would normally define like article_path(@article, "xml").

Upvotes: 3

Related Questions