Ruby-on-rails 如何从 rails 4 中的按钮调用控制器方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17243200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a controller method from a button in rails 4
提问by Slippery John
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.
所以我有一个方法需要被精确调用一次,由于这只是实验阶段,我决定一个简单的按钮就足够了。但是,我似乎无法找出如何/是否可以通过单击按钮简单地调用该方法。
The method is in home_controller.rb and the button is in index.html.erb
方法在 home_controller.rb 中,按钮在 index.html.erb 中
Any ideas? Or is this not something I can do?
有任何想法吗?还是这不是我能做的?
回答by spullen
<%= form_tag home_action_path, method: :post do %>
<%= submit_tag 'Call Action' %>
<% end %>
could also use a link
也可以使用链接
<%= link_to 'Call Action', home_action_path, method: :post %>
or you can use button_to
或者你可以使用 button_to
<%= button_to 'Call Action', home_action_path, method: :post %>
in your routes
在你的路线中
post 'home/action'
回答by David
回答by richardsonae
Another option is to create a route for your action and call it that way. Forgive me, I'm not sure what your home_controller.rbis doing. If it is the controller for your home page, I believe a pages_controller.rbwith a homemethod is more conventional for Rails.
另一种选择是为您的操作创建一个路由并以这种方式调用它。原谅我,我不确定你home_controller.rb在做什么。如果它是您主页的控制器,我相信pages_controller.rbwith ahome方法对于 Rails 来说更为常规。
If that were the case, and assuming the method you want to call is named call_action,
如果是这种情况,并且假设您要调用的方法是 named call_action,
Your pages_controller.rbwould look something like:
你pages_controller.rb会看起来像:
class PagesController < ApplicationController
#...
def call_action
<<does something really nifty>>
end
end
Your config/routes.rbwould look like:
你config/routes.rb会看起来像:
AppName::Application.routes.draw do
#...
resources :pages do
collection do
get :call_action
end
end
end
The link on your home page at views/pages/home.html.erbwould look like this:
您主页上的链接views/pages/home.html.erb如下所示:
<%= link_to "Call Action", call_action_pages_path %>
You should run rake routeson your command line to make sure you have the correct route, then stick _pathon the end of it.
您应该rake routes在命令行上运行以确保您有正确的路线,然后坚持_path到底。
If you want your link to be a button, you can create a styled classfor that and append it to your link:
如果您希望您的链接成为一个按钮,您可以为此创建一个样式类并将其附加到您的链接中:
<%= link_to "Call Action", call_action_pages_path, class: "button" %>
Or wrap it in a styled class:
或者将它包装在一个样式类中:
<div class="button">
<%= link_to "Call Action", call_action_pages_path %>
</div>
回答by Sai Ram Reddy
<%= button_to 'Invite a user', new_project_invite_path, method: :get, remote: true, class: 'btn primary' %>
If you don't want it to be an Ajax call then you can remove remote: true
如果您不希望它是 Ajax 调用,那么您可以删除 remote: true
回答by Mika Yeap
It's as easy as adding method_controller_path to the HTML.
For instance, I have a route filaments/new, and to add a button that links to it from any view:<%= button_to "Add", new_filament_path, method: :get %>
就像在 HTML 中添加method_ controller_path一样简单。
例如,我有一个 route filaments/new,并添加一个从任何视图链接到它的按钮:<%= button_to "Add", new_filament_path, method: :get %>

