Route
resources :cars do
collection do
get :f01a
end
end
Controller
class CarsController < ApplicationController
def f01a
@user = User.find(params[:id])
@count = Count.find_by_user_id(@user)
@count.increment!(:f02)
redirect_to @user
end
end
View
<%= button_to "add f01", f01a_cars_path %>
I can’t get this to work. I need to execute this code from a button.
button_tosends a POST request, but your routing is setup to only accept GET requests. You should change it to:Since you’re using
params[:id]in your action, but not sending it in at all, you’ll need to pass it in yourbutton_to:(Replace
somethingwith whatever ID you want to pass.)