Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a new tab on button click in Rails?

I have a button which navigate user to another page. (It has to be button not link). I want to open the page in new tab (or even new window!). Do I have to use JavaScript? or is there any other way! I'll appreciate your help.

Cheers.

like image 279
Venus Avatar asked Aug 01 '12 07:08

Venus


1 Answers

To expand on the answers suggesting using `target="_blank" in Rails...

If you're using the button_to helper, this is automatically wrapped in a form by Rails. It's the form tag that needs this attribute. To get it, pass it as a value in the hash of html options, for example:

button_to t(:translated_text), foo_path(foo.id), class: 'btn btn-primary', form: {target: '_blank'}

the class: and form: are elements of the html_options hash. The form: option is special because it tell Rails you want to pass the HTML option to the form tag, instead of the input tag of the button itself.

like image 144
Tom Harrison Avatar answered Nov 20 '22 00:11

Tom Harrison