已阅读关于此问题的许多问题/答案,但似乎找不到我的修复程序。路由错误 - 没有路由匹配[POST]
这是问题:我正在跟踪Rails的入门以创建一个简单的注释注册表。我的表单工作 - 可以添加新的&更新注释。然而,当我将链接添加到索引中,我得到一个路由错误:
- 此:
<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs"%>
结果:没有路由匹配[POST]“/注释/ 5” - 此:
<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs"%>
到没有路由匹配[POST] “/注释/新”
感谢您的帮助
Routes.db:
Rails.application.routes.draw do
root 'dashboard#index'
devise_for :users
resources :users, :annotations
控制器:
class AnnotationsController < ApplicationController
def index
@annotations = Annotation.all
end
def show
@annotation = Annotation.find(params[:id])
end
def new
@annotation = Annotation.new
end
def edit
@annotation = Annotation.find(params[:id])
end
def create
@annotation = Annotation.new(annotation_params)
@annotation.save
redirect_to @annotation
end
def update
@annotation = Annotation.find(params[:id])
if @annotation.update(annotation_params)
redirect_to @annotation
else
render 'edit'
end
end
def destroy
@annotation = Annotation.find(params[:id])
@annotation.destroy
redirect_to annotations_path
end
private
def annotation_params
params.require(:annotation).permit(:name, :description)
end
end
而形式(=部分)
<%= simple_form_for @annotation, url: annotations_path, html: { class: 'form-horizontal' },
wrapper: :horizontal_form,
wrapper_mappings: {
check_boxes: :horizontal_radio_and_checkboxes,
radio_buttons: :horizontal_radio_and_checkboxes,
file: :horizontal_file_input,
boolean: :horizontal_boolean
} do |f| %>
<%= f.error_notification %>
<%= f.input :name, placeholder: 'Enter name' %>
<%= f.input :description, placeholder: 'Description' %>
<%= f.input :file, as: :file %>
<%= f.input :active, as: :boolean %>
<%= f.input :choice, as: :check_boxes,
collection: [
'Option one ...',
'Option two ...'] %>
<%= f.input :documenttype, as: :radio_buttons,
collection: ['Type1', 'Type2'] %>
<%= f.button :submit %>
<% end %>
注形式:无济于事,我试图使用<%= simple_form_for :annotation, url: annotations_path,
加上'方法:GET'。 'button_to'在默认情况下执行'POST'请求,而你的路由是'GET' – kiddorails
另外,我坚持仔细研究错误日志。这个问题本身说'POST'路线不匹配,这意味着路线不存在,你可以检查为什么是这种情况。 – kiddorails
[Routing Error - 路由匹配时使用按钮\ _to与自定义操作]的可能重复(http://stackoverflow.com/questions/12650213/routing-error-no-route-matches-when-using-button-to -with-custom-action) – kiddorails