Ruby-on-rails Rails:一个表单中的多个提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3332449/
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
Rails: Multi-submit buttons in one Form
提问by kinopyo
Say I have an Article model, and in the article 'new' view I have two buttons, "Publish" and "Save Draft".
假设我有一个文章模型,在文章“新”视图中我有两个按钮,“发布”和“保存草稿”。
My question is how can I know which button is clicked in the controller.
我的问题是如何知道在控制器中单击了哪个按钮。
I already have a solution but I think there must be a better way. What I currently used in the view is:
我已经有了解决方案,但我认为必须有更好的方法。我目前在视图中使用的是:
<div class="actions">
<%= f.submit "Publish" %>
<%= f.submit "Save Draft", :name => "commit" %>
</div>
So in the controller, I can use the params[:commit]string to handle that action.
因此,在控制器中,我可以使用params[:commit]字符串来处理该操作。
def create
@article = Article.new(params[:article])
if params[:commit] == "Publish"
@article.status = 'publish'
// detail omitted
end
@article.save
end
But I think using the view related string is not good. Could you tell me another way to accomplish this?
但我认为使用与视图相关的字符串并不好。你能告诉我另一种方法来完成这个吗?
UPDATE: Since these buttons are in the same form, they're all going to the 'create' action, and that's OK for me. What I want is to handle that within the create action, such as give the Article model a 'status' column and holds 'public' or 'draft'.
更新:由于这些按钮的形式相同,它们都将执行“创建”操作,这对我来说没问题。我想要的是在创建操作中处理它,例如给文章模型一个“状态”列并保存“公开”或“草稿”。
回答by John Topley
This was covered in Railscast episode 38. Using the paramshash to detect which button was clicked is the correct approach:
Railscast 第 38 集对此进行了介绍。使用params哈希来检测哪个按钮被点击是正确的方法:
View:
看法:
<%= submit_tag 'Create' %>
<%= submit_tag 'Create and Add Another', name: 'create_and_add' %>
Controller:
控制器:
if params[:create_and_add]
# Redirect to new form, for example.
else
# Redirect to show the newly created record, for example.
end
回答by siliconsenthil
We solved using advanced constraintsin rails.
我们解决了在 rails 中使用高级约束的问题。
The idea is to have the same path (and hence the same named route & action) but with constraints routing to different actions.
这个想法是具有相同的路径(因此具有相同的命名路由和操作),但具有路由到不同操作的约束。
resources :plan do
post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize
end
CommitParamRoutingis a simple class that has a method matches?which returns true if the commit param matches the given instance attr. value.
CommitParamRouting是一个简单的类,它有一个方法matches?,如果提交参数与给定的实例属性匹配,则返回 true。价值。
This available as a gem commit_param_matching.
这可作为 gem commit_param_matching 使用。
回答by Petros Kyriakou
it can also be done on the form_forhelper like this
也可以像这样在form_for助手上完成
<%= f.submit "Publish",name: "publish", class: "tiny button radius success" %>
<%= f.submit 'Mark as Draft', name: "draft", class: "tiny button radius " %>
and the logic is the same on the controller
并且控制器上的逻辑是相同的
if params[:publish]
// your code
elsif params[:draft]
// your code
end
回答by Alok Swain
I remember coming across this problem once. You cannot keep two buttons and then call some action based on the params[:commit]. the submit button onclick is going to call the url the form refers to. There are certain bad ways to get the desired behavior. Keep a button to call the action the form refers to and to get another button to call a action, I used a link_to and then changed the styles to match a button. Also, alternatively you can use jQuery to change the url the form would call, hence deciding what action is invoked at run-time. Hope this helps.
我记得曾经遇到过这个问题。您不能保留两个按钮,然后根据 params[:commit] 调用某些操作。提交按钮 onclick 将调用表单引用的 url。有一些不好的方法可以获得所需的行为。保留一个按钮来调用表单所引用的操作并获取另一个按钮来调用一个操作,我使用了一个 link_to 然后更改了样式以匹配一个按钮。此外,您也可以使用 jQuery 来更改表单将调用的 url,从而决定在运行时调用什么操作。希望这可以帮助。
回答by dennismonsewicz
You could also set some dataattributes on the submit buttons and use JavaScript to change out the formaction on click of one of the buttons
您还可以data在提交按钮上设置一些属性,并使用 JavaScript 更改form单击其中一个按钮时的操作
回答by yonatan
usually i using the suggestion given by John Topley (see answer above). another way is using JQuery /JS changing the form action attribute- upon clicking the submit button example:
通常我使用 John Topley 给出的建议(见上面的答案)。另一种方法是使用 JQuery /JS 更改表单操作属性 - 单击提交按钮示例:
form_tag({} ,:method => 'post', :id => 'reports_action') do
.......
.......
submit_tag 'submit', :onclick => "return changeAction();"
end
and then .....
进而 .....
function changeAction(){
$('#reports_action').attr('action','my_new_action');
}

