San Backups
San Backups

Reputation: 503

Simple Ruby on Rails Button Action

I have a method in a helper file that I want activated only when a button is pressed.

def add_f01
    @count = Count.find_by_user_id(@user)
    @car = Car.find_by_user_id(@user)

    @car.toggle!(:f01)
    @count.increment!(:v01)
end

How do I do it, please?

Upvotes: 1

Views: 4021

Answers (1)

Noah Clark
Noah Clark

Reputation: 8131

I've created a working app here: https://github.com/noahc/stackoverflow

Pull it down and play around with it so you can learn how it works.

Essentially you need the following:

#routes.rb

match 'f01', to: 'users#call_app_controller'


# Anywhere in your view. I have it in index.html.erb of users
<td><%= button_to 'change name', f01_path(user: user)%></td>

#Application controller
def add_f01(user)
  user.first = "changed in Application Controller"
  user.save
end

 #users_controller
 def call_app_controller
  @user = User.find(params[:user])
  add_f01(@user)
  redirect_to users_path
end

Upvotes: 2

Related Questions