另一个Rails没有路线matche [Patch]

时间:2015-05-15 21:37:48

标签: ruby-on-rails ruby routes blogs patch

我正在尝试编辑文章并重新保存。我可以创建一篇新文章并保存。我甚至可以对这篇文章发表评论并保存。当我去编辑文章时,它会自动使用要编辑的文本填充表单,但是当我尝试保存它时,它会给我“无路由匹配[补丁]”/ artices.7“错误。

文章控制器

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

评论控制器

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

路由

Rails.application.routes.draw do

  resources  :articles do
  resources :comments
 end
end

当我运行编辑博客edit.html.erb时,它给了我“没有路线匹配[补丁]错误。我应该以不同的方式做我的路线吗?我认为资源会覆盖它。这是编辑文件

   <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 %>

更新了佣金路线

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

1 个答案:

答案 0 :(得分:3)

我认为我在您指定的form_for声明articles_path而不是article_path中找到了它。这是两种不同的方法,他们期望不同的参数。请改用article_path,您应该得到预期的结果。

此外,这在计算机处理方面没有任何区别,但您应该将实例变量命名为@article而不是@articles,因为它仅指一个对象,而不是对象的列表。当你使用Rails(特别是路由,控制器名称,模型名称等)时,你会注意到框架在你使用单数时和使用复数形式时非常挑剔,这当然是为什么你做了如上所述,使用articles_path的错误。

Rails使它更令人沮丧,因为他们没有告诉你你使用了错误的方法。 articles_path并不需要文章ID,因此理想情况下,Rails会在您提供错误时引发错误,但事实证明所有_path_url路径帮助程序方法都接受附加变量,以便您可以定义URL的文件扩展名(通常定义为article_path(@article, "xml")