【问题标题】:Another Rails No route matche [Patch]另一个 Rails 没有路由匹配 [Patch]
【发布时间】:2015-05-15 21:37:48
【问题描述】:

我正在尝试编辑一篇文章并重新保存它。我可以创建一篇新文章并保存它。我什至可以评论文章并保存。当我去编辑文章时,它会自动用要编辑的文本填充表单,但是当我尝试保存它时,它会给我“没有路由匹配 [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 时,它给了我“没有路由匹配 [Patch] 错误。我应该以不同的方式执行我的路由吗?我认为资源会涵盖这一点。这是编辑文件

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

更新了 rake 路线

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

【问题讨论】:

  • 您能否在控制台中运行rake routes 并将输出复制粘贴到您的问题中

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


【解决方案1】:

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

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

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

【讨论】:

  • 我以为我已经检查过了,这就是我决定在这里发帖的原因,但它奏效了。 (一巴掌)这个简单的东西......
  • 是的,你应该知道的数百种特殊方法和关键字真的加起来了。多年使用 Rails 的经验,当您赶时间时,它仍然可以帮助您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
  • 2016-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多