调用方法而不打开show view - Rails

时间:2016-11-10 21:22:48

标签: ruby-on-rails

我有一个方法,我想从控制器运行,这应该通过单击按钮来完成,我的问题是,当我点击特定的行或项目时,我会重定向到该项目的视图。

这是我创建的路线: patch '/withdraw/:id', to: 'payments#withdraw', as: :withdraw

这是link_to行: <%= link_to 'Withdraw', withdraw_path(pay), method: :post %>

这是方法,实际上是更新现有属性:

def withdraw
 @payment = Payment.find(params[:id])
 @payment.withdrawn = true
 @payment.amount_interest = @payment.amount * 1.1
 @payment.save
end

为了获得理想的结果,我需要更改什么?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

remote: true添加到链接。

<%= link_to 'Withdraw', withdraw_path(pay), method: :patch, remote: true %>

并在操作结束时添加head :ok

def withdraw
  ..
  head :ok
end