San Backups
San Backups

Reputation: 503

Button to Execute Ruby Without Javascript

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.

Upvotes: 0

Views: 120

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96954

button_to sends a POST request, but your routing is setup to only accept GET requests. You should change it to:

resources :cars do
  collection do
    post :f01a
  end
end

Since you're using params[:id] in your action, but not sending it in at all, you'll need to pass it in your button_to:

<%= button_to "add f01", f01a_cars_path(:id => something)

(Replace something with whatever ID you want to pass.)

Upvotes: 2

Related Questions