当用户点击使用此_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
感谢您的帮助!
答案 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
请注意从post
到patch
另一种解决方案是在不更改method
的情况下在表单中指定post
到routes
:
<%= simple_form_for @routine, url: create_freebie_routine_path(params[:id]), method: :post do |f| %>
<%= f.date_select %>
<% end %>