[英]How to call a controller method from a button in 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.
但是,我似乎无法找到如何/如果我可以通过按钮单击调用该方法。
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?
或者这不是我能做的事情吗?
<%= 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'
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.rb
is doing. 原谅我,我不确定你的
home_controller.rb
在做什么。 If it is the controller for your home page, I believe a pages_controller.rb
with a home
method is more conventional for Rails. 如果它是你的主页的控制器,我相信带有
home
方法的pages_controller.rb
对于Rails更为传统。
If that were the case, and assuming the method you want to call is named call_action
, 如果是这种情况,并假设您要调用的方法名为
call_action
,
Your pages_controller.rb
would look something like: 您的
pages_controller.rb
看起来像:
class PagesController < ApplicationController
#...
def call_action
<<does something really nifty>>
end
end
Your config/routes.rb
would 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.erb
would look like this: 您在home
views/pages/home.html.erb
主页上的链接如下所示:
<%= link_to "Call Action", call_action_pages_path %>
You should run rake routes
on your command line to make sure you have the correct route, then stick _path
on the end of it. 您应该在命令行上运行
rake routes
以确保您具有正确的路由,然后在其末尾粘贴_path
。
If you want your link to be a button, you can create a styled class for 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>
<%= button_to 'Invite a user', new_project_invite_path, method: :get, remote: true, class: 'btn primary' %>
如果您不希望它是Ajax调用,那么您可以删除remote:true
It's as easy as adding method _ controller _path to the HTML. 它就像将方法 _ controller _path添加到HTML一样简单。
For instance, I have a route filaments/new
, and to add a button that links to it from any view: 例如,我有一条路线
filaments/new
,并添加一个从任何视图链接到它的按钮:
<%= button_to "Add", new_filament_path, method: :get %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.