ruby-on-rails - 另一个 Rails 没有路由匹配 [补丁]

标签 ruby-on-rails ruby routes blogs patch

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

文章 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

评论 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

路线

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

更新了佣金路线

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

最佳答案

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

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

Rails 更令人沮丧,因为他们没有通知您您使用了错误的方法。 articles_path 不需要文章 ID,因此理想情况下,Rails 会在您提供文章 ID 时引发错误,但结果是所有 _path_url 路由帮助器方法接受一个附加 变量,因此您可以定义 URL 的文件扩展名(您通常定义为 article_path(@article, "xml") .

关于ruby-on-rails - 另一个 Rails 没有路由匹配 [补丁],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30269135/

相关文章:

ruby-on-rails - 我无法在 Ruby on Rails 上提供静态图像

php - Symfony2 新的捆绑路由

python - Django 路由问题

ruby-on-rails - Rails 3 UJS 和 XHTML

ruby-on-rails - Cucumber:Factory Girl 不修改数据库——使用 .create 创建对象但之后数据库表仍然为空

ruby-on-rails - Rake:记录任何正在执行的任务

ruby - 解析单个键/值的字符串的 "ruby way"是什么?

html - 在我的域服务器上使用 Ruby on Rails 应用程序

json - Ruby - 尝试使用嵌套哈希迭代哈希(在反序列化 JSON 对象之后)

ruby-on-rails - Rails 3 带有斜线和嵌套资源的路线