问题
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