【发布时间】: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