我在帮助文件中有一个方法,我只想在按下按钮时激活它。
def add_f01
@count = Count.find_by_user_id(@user)
@car = Car.find_by_user_id(@user)
@car.toggle!(:f01)
@count.increment!(:v01)
end
我该怎么办?
答案 0 :(得分:2)
我在这里创建了一个有效的应用:https://github.com/noahc/stackoverflow
将其拉下并玩弄它,这样你就可以了解它是如何工作的。
基本上你需要以下内容:
#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