Ruby on rails rails:ActionController::RoutingError(没有与[PATCH]“article/viewing-article-2”匹配的路由):
我目前正在学习rails,面临着一个完全奇怪的问题。 我正试图按照我正在做的教程来更新一篇现有的文章,但是它没有得到更新。我分别从终端和浏览器收到以下错误:Ruby on rails rails:ActionController::RoutingError(没有与[PATCH]“article/viewing-article-2”匹配的路由):,ruby-on-rails,ruby,Ruby On Rails,Ruby,我目前正在学习rails,面临着一个完全奇怪的问题。 我正试图按照我正在做的教程来更新一篇现有的文章,但是它没有得到更新。我分别从终端和浏览器收到以下错误: - Terminal: * Started PATCH "/article/viewing-article-2" for 127.0.0.1 at 2018-03-28 18:54:46 +0200 * No route matches [PATCH] "/article/viewing-article-2" - Browser: Act
- Terminal:
* Started PATCH "/article/viewing-article-2" for 127.0.0.1 at 2018-03-28 18:54:46 +0200
* No route matches [PATCH] "/article/viewing-article-2"
- Browser: ActionController::RoutingError (No route matches [PATCH] "/article/viewing-article-2"):
我的路线是
**routes.erb**
Rails.application.routes.draw do
root to: 'pages#index'
post 'article/create-new-article', to: 'pages#create'
get 'article/viewing-article-:id', to: 'pages#show', as: 'article'
get 'article/:id/edit', to: 'pages#edit', as: 'article_edit'
patch 'article/:id/update', to: 'pages#update', as: 'update_article'
get 'article/new-article', to: 'pages#new'
get 'article/destroy', to: 'pages#destroy'
end
和我的控制器:
controller.erb
def index
@articles = @@all_articles.all
end
def show
@article = Article.find(params[:id])
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
article_params = params.require(:article).permit(:title, :author, :publisher, :content)
@article.update(article_params)
redirect_to root_path
end
和我的html:
edit.html.erb
<% content_for :title do %>Editing <%= @article.title %><% end %>
<% content_for :bodycontent do %>
<h3>Editing <%= @article.title %></h3>
<%= form_for @article do |f| %>
<%= f.text_field :title, class: 'form-control'%>
<%= f.text_field :author, class: 'form-control'%>
<%= f.text_field :publisher, class: 'form-control'%>
<%= f.text_area :content, class: 'form-control'%>
<%= f.submit class: 'form-control btn btn-primary' %>
<% end %>
<% end %>
edit.html.erb
编辑
编辑
我不确定我做错了什么,因为所选文章没有更新
到目前为止,我一直喜欢rails,并希望在这方面做得更好。非常感谢您的帮助。首先,您确实应该使用适合您的文章模型:
Rails.application.routes.draw do
root 'pages#index'
resources :articles
end
这就是控制器的外观。关于许可证参数,请阅读
使用自定义url修改的表单,如下所示
<%= form_for @article, url: @article.new_record? ? article_create_new_article_path : update_article(@post)do |f| %>
但我建议您改用足智多谋的路由。您好。感谢您的回复。您坚持使用资源有什么具体原因吗?如果您更愿意使用客户url名称,就像在django和laravel中一样,请基本上美化您的url。你怎样才能正确地实现它rails@Marie因为这是一个安静的:)有很多理由说明这是好的。只需在google中尝试“RESTful为什么”。@Marie此外,您还可以使用另一个唯一的参数,如
resources:articles,param::path\u name
Hi。感谢您的回复。您坚持使用资源有什么具体原因吗?如果您更愿意使用客户url名称,就像在django和laravel中一样,请基本上美化您的url。你如何在Rails中正确实现它我不知道laravel和django,你想显示一些文本而不是id吗?比如说“文章/查看文章idplease check gem可能适合你”。
<%= form_for @article, url: @article.new_record? ? article_create_new_article_path : update_article(@post)do |f| %>