# Adding an onclick option to link\_to method

**URL:** <https://www.ruby-forum.com/t/adding-an-onclick-option-to-link-to-method/129555>\
**Category:** Rails\
**Created:** [February 19, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/adding-an-onclick-option-to-link-to-method/129555 "2008-02-19T16:38:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tblack06](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tblack06](https://www.ruby-forum.com/u/tblack06)\
**Post date:** [February 19, 2008, 4:38pm UTC](https://www.ruby-forum.com/t/adding-an-onclick-option-to-link-to-method/129555/1 "2008-02-19T16:38:25Z")

</div>

Hello,

I’m trying to use the rail link\_to to open a popup window and then  
print the popup window at the same time. Is there a way to add  
another “onclick” option to tell the popup to print? I know how to do  
this in javascript but not sure how to use the rails help to do it. I  
also am aware that the link\_to\_function method is used to call a  
javascript function. Here’s the code I have to open the popup:

\<%= link\_to icon(‘icon\_printer’, :alt =\> ‘Print’) + ‘Print Cable’,  
{:controller =\> ‘print\_popup’,  
:action =\> ‘display\_popup’,  
:doc =\> @document,  
:header =\> ‘My Doc’},  
:title =\> ‘My Title’,  
:popup =\> [‘display\_popup\_to’,  
‘width=650,height=600,scrollbars=yes, menubar=1’]%\>

---

<div class="post-metadata">

**Author:** ![tblack06](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/f07891/32.png) [@tblack06](https://www.ruby-forum.com/u/tblack06)\
**Post date:** [February 19, 2008, 7:31pm UTC](https://www.ruby-forum.com/t/adding-an-onclick-option-to-link-to-method/129555/2 "2008-02-19T19:31:15Z")

</div>

tblack06 wrote:

> Hello,
> 
> I’m trying to use the rail link\_to to open a popup window and then  
> print the popup window at the same time. Is there a way to add  
> another “onclick” option to tell the popup to print? I know how to do  
> this in javascript but not sure how to use the rails help to do it. I  
> also am aware that the link\_to\_function method is used to call a  
> javascript function. Here’s the code I have to open the popup:
> 
> \<%= link\_to icon(‘icon\_printer’, :alt =\> ‘Print’) + ‘Print Cable’,  
> {:controller =\> ‘print\_popup’,  
> :action =\> ‘display\_popup’,  
> :doc =\> @document,  
> :header =\> ‘My Doc’},  
> :title =\> ‘My Title’,  
> :popup =\> [‘display\_popup\_to’,  
> ‘width=650,height=600,scrollbars=yes, menubar=1’]%\>

Here is how I did mine:

\<%=link\_to\_function “Print”,“window.print();” %\>

But I had to fist generate the popup window using another link:

\<%=link\_to “Print Voids  
List”,{:action=\>“print\_voids”},:popup=\>[‘new\_window’,‘height=300,width=600’,‘scrollbars=yes’]%\>

Maybe?:  
\<%= link\_to\_function icon(‘icon\_printer’, :alt =\> ‘Print’) + ‘Print  
Cable’, :action = ‘display\_popup’, :popup =\> [‘display\_popup\_to’,  
‘width=650,height=600,scrollbars=yes, menubar=1’], “window.print();” %\>

This is purely a guess though, I haven’t tried it out. But the  
“window.print()” part does work on my app, I think you just have to find  
a way to combine the two. Good luck,

-S
