# Get text box data after pressing button in plugin

**URL:** https://meta.discourse.org/t/get-text-box-data-after-pressing-button-in-plugin/337702
**Category:** Development
**Created:** [November 22, 2024, 8:18am UTC](https://meta.discourse.org/t/get-text-box-data-after-pressing-button-in-plugin/337702 "2024-11-22T08:18:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [November 22, 2024, 8:18am UTC](https://meta.discourse.org/t/get-text-box-data-after-pressing-button-in-plugin/337702/1 "2024-11-22T08:18:48Z")

</div>

I’m working on my first plugin, and I want to be able to get the text content of a text box in the admin panel (part of the plugin) after pressing a button. Then, I want to do some stuff with the content in Ruby. Is there a way for the Ember frontend to send data back to the Ruby backend?  
My `plugin.rb`:

```ruby
# name: Discourse Sample Plugin
# about: A simple plugin to play around with
# version: 1.0.0
# authors: NateDhaliwal 
# url: https://google.com

enabled_site_setting :plugin_enabled

add_admin_route 'sample_page.title', 'sample-page'

Discourse::Application.routes.append do
  get '/admin/plugins/sample-page' => 'admin/plugins#index', constraints: StaffConstraint.new
end

```

My `assets/javascripts/discourse/templates/admin/plugin-sample-plugin.hbs`:

```hbs
<div class="textbox">
  <input id="textbox-data" type="text" />
</div>

<div class="buttons">
  <DButton
    @label="sample_page.send"
    @action={{action "sendTextboxData"}}
    @icon"arrow-right"
    @id="send-textbox-data"
  />
</div>

```

Not sure what by `sendTextboxData` should do.  
My `assets/javascripts/discourse/sample-page-route-map.js`:

```js
export default {
  resource: 'admin.adminPlugins',
  path: '/plugins',
  map() {
    this.route('sample-page');
  }
};

```

My `assets/javascripts/discourse/controllers/admin-plugins-sample-plugin.js.es6`:

```js
import Controller from "@ember/controller";
import { action } from "@ember/object";

export default class AdminPluginsSamplePlugin extends Controller {

  @action
  sendTextboxData() {
    // How do I send the data?
  }
}

```

If I do manage to get the data to the Ruby backend, where do I put the logic for dealing with the data? In `plugin.rb`? Or some other file?

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [November 23, 2024, 7:32am UTC](https://meta.discourse.org/t/get-text-box-data-after-pressing-button-in-plugin/337702/2 "2024-11-23T07:32:15Z")

</div>

Or would a setting be more ideal? However, my use case is that the textbox won’t be needing to retain values each time. Does yml provide this functionality? Can I change Site Settings through Ruby?

---

<div class="post-metadata">

### Author: ![Alteras](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alteras/32/179824_2.png) [@Alteras](https://meta.discourse.org/u/Alteras)
#### Post date: [November 23, 2024, 9:05pm UTC](https://meta.discourse.org/t/get-text-box-data-after-pressing-button-in-plugin/337702/3 "2024-11-23T21:05:29Z")

</div>

It sounds like what you’re looking for is to make an API in Ruby that the JS can call. An example is the [Data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin, which implements a user input API with admin permission scope on the ruby side.

> <https://github.com/discourse/discourse-data-explorer/blob/main/config/routes.rb#L3-L16>

> <https://github.com/discourse/discourse-data-explorer/blob/main/app/controllers/discourse_data_explorer/query_controller.rb>

The JS for the [data explorer](https://meta.discourse.org/t/32566?silent=true) is on the beefier side of things, so I’ll just give a simplified example of what you’d probably need.

```js
import { ajax } from "discourse/lib/ajax";

...
  @action
  sendTextboxData() {
    ajax("/endpoint", {
        type: "POST",
        data: {
          key: "value"
        },
    }).then((result) => {
        // do something with the result
    };
  }

```

You can do the same thing with a native JS `fetch()` but it seems that the `ajax()` function that discourse provides handles a few minor things under the hood.

If your ruby code is light enough, you can probably fit it entirely within a single file for the controller. Otherwise you can organize it however you need.
