# How to execute server-side code when clicking a button?

**URL:** https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795
**Category:** Development
**Created:** [May 19, 2023, 3:03pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795 "2023-05-19T15:03:07Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Yogesh\_Kbizsoft](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yogesh_kbizsoft/32/297602_2.png) [@Yogesh\_Kbizsoft](https://meta.discourse.org/u/Yogesh_Kbizsoft)
#### Post date: [May 19, 2023, 3:03pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/1 "2023-05-19T15:03:07Z")

</div>

How can I do something like,  
When I click on a custom button, I want to run some server side code.

For instance One button click I want to run file Jobs/Secheduled/test\_job.rb

Or Can I run the code inside this file when somebody visits the url admin/plugins/my-plugin.

I am a beginner to discourse plugin development, And really struggling to do something like this, Any help will be much appricated

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 20, 2023, 10:22am UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/2 "2023-05-20T10:22:34Z")

</div>

> [@Yogesh\_Kbizsoft](#):
>
> When I click on a custom button, I want to run some server side code.

Then you’ll need to make an Ajax request to an endpoint that runs that code.

Can you say more about what you want to happen on the server side?

---

<div class="post-metadata">

### Author: ![Yogesh\_g](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yogesh_g/32/253636_2.png) [@Yogesh\_g](https://meta.discourse.org/u/Yogesh_g)
#### Post date: [May 20, 2023, 11:51am UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/3 "2023-05-20T11:51:46Z")

</div>

I want execute the code inside file Jobs/Secheduled/test\_job.rb

Or you can say I want to run a cron job.

Can you please show me an example code how can I make ajax request to an endpoint and execute the code inside the file.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 20, 2023, 12:09pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/4 "2023-05-20T12:09:18Z")

</div>

You’re still describing your solution and not the problem you’re solving. What do you want to do in that file? There may be a better way to trigger it. Knowing that will also make it easier to suggest something similar.

The subscription plugin adds an endpoint, so you might look at that. You’ll need to add a route in rails and then in ember add an action to your button that will call it.

---

<div class="post-metadata">

### Author: ![Yogesh\_g](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yogesh_g/32/253636_2.png) [@Yogesh\_g](https://meta.discourse.org/u/Yogesh_g)
#### Post date: [May 20, 2023, 12:27pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/5 "2023-05-20T12:27:22Z")

</div>

I have a user with huge number of posts. I want to delete all posts of for that user the default delete all posts button giving 503 error. So I written a code schedule a cron job to run every 2 minutes and delete 100 posts at a time

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 20, 2023, 1:42pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/6 "2023-05-20T13:42:47Z")

</div>

Just run it at the rails console and don’t write a plugin.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [May 20, 2023, 2:14pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/7 "2023-05-20T14:14:45Z")

</div>

Use the language features and do as @pfaffman suggests. Writing a bespoke front end hook to achieve this is seriously OTT.

See: [ActiveRecord::Batches](https://api.rubyonrails.org/v7.0.4.2/classes/ActiveRecord/Batches.html)

---

<div class="post-metadata">

### Author: ![Yogesh\_g](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yogesh_g/32/253636_2.png) [@Yogesh\_g](https://meta.discourse.org/u/Yogesh_g)
#### Post date: [May 20, 2023, 2:17pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/8 "2023-05-20T14:17:00Z")

</div>

I am not familiar to rails. Can you show me the command to delete posts for that user

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [May 20, 2023, 10:19pm UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/9 "2023-05-20T22:19:30Z")

</div>

You can try searching for `PostDestroyer`

---

<div class="post-metadata">

### Author: ![Yogesh\_g](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yogesh_g/32/253636_2.png) [@Yogesh\_g](https://meta.discourse.org/u/Yogesh_g)
#### Post date: [May 22, 2023, 5:58am UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/10 "2023-05-22T05:58:50Z")

</div>

Yes, you are but there are many such users on the site. And my client is non-technical person how will he that.I need to write a plugin as the client institng me

And also the issue can arises in other forums website. We really need to have a look at the issue I am facing. we can make improvements in default delete post feature in future update

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [June 13, 2023, 8:29am UTC](https://meta.discourse.org/t/how-to-execute-server-side-code-when-clicking-a-button/265795/13 "2023-06-13T08:29:39Z")

</div>

13 posts were merged into an existing topic: [I am getting 502 error while trying to delete topics created by specific user](https://meta.discourse.org/t/i-am-getting-502-error-while-trying-to-delete-topics-created-by-specific-user/261161/18)
