【问题标题】:Routing error - No route matches [POST]路由错误 - 没有路由匹配 [POST]
【发布时间】:2016-09-01 09:22:51
【问题描述】:

已阅读有关此问题的许多问题/答案,但似乎没有找到我的解决方法。

这是问题所在:我正在按照 Rails 的入门指南创建一个简单的注解寄存器。我的表单工作 - 可以添加新的和更新注释。然而,当我向索引添加链接时,出现路由错误:

  • 这:<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs"%> 导致:没有路由匹配 [POST] "/annotations/5"
  • 这个:<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs"%>没有路由匹配 [POST] "/annotations/new"

感谢您的帮助

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

表格注意:无济于事,我尝试使用&lt;%= simple_form_for :annotation, url: annotations_path,

【问题讨论】:

标签: ruby-on-rails ruby-on-rails-5


【解决方案1】:

问题是因为 button_to 助手实际上在代码级别生成了一个表单,因此正在发布该表单,但是新资源的路由必须是 GET。

button_to 标签确实不应该用于 GET 请求,所以我会使用带有 CSS 类的 link_to(你已经有了必要的类),但如果你愿意,你可以使用下面的方法:

<%= button_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs", method: :get%>

然而,更好的方法是:

<%= link_to "Details", annotation_path(annotation), class: "btn btn-primary btn-xs" %>

【讨论】:

  • 更好的方法的好建议。在什么情况下我应该使用 button_to?仅用于发布?
  • 我会说你也应该将它用于 PUT\PATCH(更新)操作,但这需要在帮助器上指定方法,如上面 i/e :patch 或 :put。
【解决方案2】:

在此处阅读文档button_to

方法默认是:post改用:method参数

<%= button_to "Details", annotation_path(annotation), :class => "btn btn-primary btn-xs", method: :get%>

<%= button_to "Add Annotation", new_annotation_path, :class => "btn btn-primary btn-xs", method: :get%>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多