Can I add a value to the submit button, which is passed in the parameters?

I am trying to add a preview button / page to my application.

Is there a way to add another value to both my "post" and the "preview" submit buttons that are 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?

+6
source share
2 answers

The params hash keys are simply the name value of the element.

If you have two buttons with the name "submit", one with value="post" and value="preview" , you can do something like:

 if params[:submit] == "preview" 
+8
source

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

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

Then you will access the action in your controller using params['form_action']

+3
source

Source: https://habr.com/ru/post/890530/


All Articles