# Button / link to run a method

**URL:** <https://www.ruby-forum.com/t/button-link-to-run-a-method/180072>\
**Category:** Rails\
**Created:** [December 10, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/button-link-to-run-a-method/180072 "2009-12-10T21:08:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 10, 2009, 9:08pm UTC](https://www.ruby-forum.com/t/button-link-to-run-a-method/180072/1 "2009-12-10T21:08:16Z")

</div>

There are two questions - one, is this possible, two, is this a good  
idea (and if not, then three : how is it a good idea to implement this).

1. I have a ruby method in a file under lib/ . It is included in  
environment.rb. It is called, and runs, every hour, using  
rufus-scheduler. I would like to add to one of my views a button or a  
link which would allow one to run this method manually. I haven’t found  
a way to do this (link\_to, link\_to\_remote, link\_to\_function, etc. etc.)  
Can this be done?

2. Is this even a good idea / implementation ?

3. What _is_ a good way to implement this?

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 11, 2009, 3:40pm UTC](https://www.ruby-forum.com/t/button-link-to-run-a-method/180072/2 "2009-12-11T15:40:16Z")

</div>

Robert W. wrote:

> > 
> 
> You could add a controller with the appropriate route (in  
> config/routes.rb) containing an action method that in turn calls your  
> method on your library class.
> 
> It would even be better, depending on how your background processor  
> works, to use the controller action to schedule the task for execution  
> rather than calling the method directly.
> 
> The delayed\_job gem has a convenient way to do this with with  
> #send\_later(method, params).
> 
> [GitHub - collectiveidea/delayed\_job: Database based asynchronous priority queue system -- Extracted from Shopify](http://github.com/collectiveidea/delayed_job)

Alright - so basically, stick to the basic Rails idea… Convention over  
Configuration, right? 🙂  
Thanks for the delayed\_job idea - but as I said, it runs every hour  
already. I don’t see much of a point in adding that as a delayed job (I  
expect it takes a whopping 10 seconds to run, maybe 15 if the wind is  
contrary).

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 10, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/button-link-to-run-a-method/180072/3 "2009-12-10T23:14:19Z")

</div>

Aldric G. wrote:

> There are two questions - one, is this possible, two, is this a good  
> idea (and if not, then three : how is it a good idea to implement this).
> 
> 1. I have a ruby method in a file under lib/ . It is included in  
> environment.rb. It is called, and runs, every hour, using  
> rufus-scheduler. I would like to add to one of my views a button or a  
> link which would allow one to run this method manually. I haven’t found  
> a way to do this (link\_to, link\_to\_remote, link\_to\_function, etc. etc.)  
> Can this be done?

You could add a controller with the appropriate route (in  
config/routes.rb) containing an action method that in turn calls your  
method on your library class.

> 1. Is this even a good idea / implementation ?

It’s not a terrible idea as long as the method does not block other  
requests while executing for too long.

> 1. What _is_ a good way to implement this?

It would even be better, depending on how your background processor  
works, to use the controller action to schedule the task for execution  
rather than calling the method directly.

The delayed\_job gem has a convenient way to do this with with  
#send\_later(method, params).

> **[GitHub - collectiveidea/delayed\_job: Database based asynchronous priority...](https://github.com/collectiveidea/delayed_job)**
>
> Database based asynchronous priority queue system -- Extracted from Shopify - GitHub - collectiveidea/delayed\_job: Database based asynchronous priority queue system -- Extracted from Shopify
