如何从 rails 4 中的按钮调用控制器方法 [英] How to call a controller method from a button in rails 4

查看:36
本文介绍了如何从 rails 4 中的按钮调用控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我现在觉得很愚蠢,但我似乎找不到答案.

So I feel really stupid right now, but I can't seem to find an answer.

所以我有一个方法需要被精确调用一次,由于这只是实验阶段,我决定一个简单的按钮就足够了.但是,我似乎不知道如何/是否可以通过单击按钮简单地调用该方法.

So I have a method which needs to be called EXACTLY once, and since this is only the experimental phase, I decided that a simple button should suffice. However, I can't seem to find out how to / if I can simply call the method from a button click.

方法在 home_controller.rb 中,按钮在 index.html.erb 中

The method is in home_controller.rb and the button is in index.html.erb

有什么想法吗?或者这不是我能做的吗?

Any ideas? Or is this not something I can do?

推荐答案

<%= form_tag home_action_path, method: :post do %>
  <%= submit_tag 'Call Action' %>
<% end %>

也可以使用链接

<%= link_to 'Call Action', home_action_path, method: :post %>

或者你可以使用 button_to

or you can use button_to

<%= button_to 'Call Action', home_action_path, method: :post %>

在您的路线中

post 'home/action'

这篇关于如何从 rails 4 中的按钮调用控制器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆