路由错误! “ActionController :: RoutingError(没有路由匹配[PATCH]”

时间:2016-01-28 00:08:31

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

当用户点击使用此_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,然后控制器会通过freebie操作将+1 create_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

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您必须更换此

  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

请注意从postpatch

的更改

另一种解决方案是在不更改method的情况下在表单中指定postroutes

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

  <%= f.date_select %>

<% end %>