Is it possible to add a value to a submit button that gets passed in the params?

我的未来我决定 提交于 2020-01-02 03:46:08

问题


I'm trying to add a "preview" button/page to my app.

Is there a way to add a different value to both my "post" and "preview" submit buttons that get passed in the params hash so that I can check which one was pressed in the controller and render the view accordingly?

Is this the best way to do this?


回答1:


The keys of the params hash are just the name value of an element.

If you had two buttons named "submit", one with value="post" and value="preview", then you could do something like:

if params[:submit] == "preview"



回答2:


You can add a button click event with javascript that will fill a hidden value and pass it along with the rest of your parameters. If using jQuery:

$("#post_button").click(function() {
   $("#form_action").val("post");
});

$("#preview_button").click(function() {
   $("#form_action").val("preview");
});

You'd then access the action in your controller using params['form_action']



来源:https://stackoverflow.com/questions/6348223/is-it-possible-to-add-a-value-to-a-submit-button-that-gets-passed-in-the-params

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!