【问题标题】:Routing Error! "ActionController::RoutingError (No route matches [PATCH]"路由错误! “ActionController::RoutingError(没有路由匹配 [PATCH]”
【发布时间】:2016-01-28 00:08:30
【问题描述】:

当用户使用这个_form点击提交时:

<%= simple_form_for(@routine, url: create_freebie_routine_path(params[:id])) do |f| %>

  <%= f.date_select %>

<% end %>

他收到此错误:ActionController::RoutingError (No route matches [PATCH] "/routines/2/create_freebie")

我不知道如何解决它。这个想法是,用户将提交上面输入的freebie_date _form,然后控制器将通过create_freebie 操作将+1 freebie 添加到更新的@routine

路线

  resources :routines do
    member do
      get :new_freebie
      post :create_freebie
    end
  end

routines_controller

  def new_freebie
    @routine = current_user.routines.find(params[:id])
    @routine.freebie_date = Date.yesterday
    respond_modal_with @routine
  end

  def create_freebie
    @routine = current_user.routines.find(params[:id])
    @routine.freebie = @routine.freebie + 1
    @routine.save
    respond_modal_with @routine, location: root_path
    flash[:alert] = 'Freebie added'
  end

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller routing


    【解决方案1】:

    你必须替换它

      resources :routines do
        member do
          get :new_freebie
          post :create_freebie
        end
      end
    

    与:

      resources :routines do
        member do
          get :new_freebie
          patch :create_freebie
        end
      end
    

    注意从 post 到 patch 的变化


    另一种解决方案是在表单中指定method 到post,而不更改您的routes:

    <%= simple_form_for @routine, url: create_freebie_routine_path(params[:id]), method: :post do |f| %>
    
      <%= f.date_select %>
    
    <% end %>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 1970-01-01
      • 2016-08-25
      • 2018-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多