# Using tag-chooser in a plugin

**URL:** https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656
**Category:** Development
**Created:** [August 1, 2019, 7:14pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656 "2019-08-01T19:14:40Z")
**Posts on this page:** 20
**Page:** 1

<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: [August 1, 2019, 7:14pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/1 "2019-08-01T19:14:40Z")

</div>

I’m working on a plugin that will assign a default tag to new topics created in a category.

The below code “works” if I enter tag names separated by “|”, so “tag1|tag2” works. (I can hardly believe it! But I digress.)

```plaintext
<h3>{{i18n 'topic_default_tag.title'}}</h3>
<section class='field default-tag'>
  <div class="default-tag">
  <label>
    {{input type="list" value=category.custom_fields.default_tag }}
    {{popup-input-tip validation=tagValidation}}
    {{i18n 'topic_default_tag.default_tag'}}
  </label>
  </div>
</section>

```

What I’d like now is to have a proper tag selector rather than an un-validated string. It seems like the below should work:

```plaintext
<h3>{{i18n 'topic_default_tag.title'}}</h3>
<section class='field default-tag'>
  <div class="default-tag">
  <label>
    {{tag-chooser tags=category.custom_fields.default_tag tabindex="4" categoryId=category.id}}
    {{popup-input-tip validation=tagValidation}}
    {{i18n 'topic_default_tag.default_tag'}}
  </label>
  </div>
</section>

```

But it doesn’t. The basis of my problem is that I have nearly zero idea how the magic of those handlebars works. Like I guess that the first section works because it’s magically getting the field name from `default-tag` in the `<section class='field default-tag'>`, but that was lucky. 🙂

When I use the `tag-chooser` the tags get passed to Rails as an array, which it throws away before I can make them into a `|`-delimited string to stick into the CategoryCustomField. With the `{{input type=list...}}` I can enter the `|`-delimited string myself and it works just fine. Do I need some kind of Ember magic to conver the array into a string on the Ember side?

Maybe I need to do something like [What's the best approach to access category specific settings?](https://meta.discourse.org/t/whats-the-best-approach-to-access-category-specific-settings/40728)?

EDIT: To add tags before the webhook gets called, use `after_create` rather than `DiscourseEvent.on(:post_created)`. Rails mostly makes sense to me now, but Ember, Javascript, and CSS, not so much.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 1, 2019, 8:29pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/2 "2019-08-01T20:29:48Z")

</div>

you can check if the variables you’re using contain the values you need by using `{{log variable_name}}` and checking the console. If the correct values aren’t passed to the component, then it wouldn’t show the correct output.

You can verify them by doing the same in the core code to confirm whether the values are passed correctly.

Forgive me if it sounds naive to you.

---

<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: [August 1, 2019, 8:43pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/3 "2019-08-01T20:43:33Z")

</div>

> [@fzngagan](#):
>
> Forgive me if it sounds naive to you.

No! I’m just a caveman.

That’s very helpful, as I didn’t know how to do that. As it turns out, I **do** have data in those fields. **AND** , the tag name data **is** in `category.custom_fields.default_tag` and getting passed to the `mini-tag-chooser`. `category.id` does have the ID of a category, but the chooser isn’t offering any tags.

I can’t tell whether if it were that data would get pushed into the field.

EDIT: OOOOH! But using `tag-chooser` instead of `mini-tag-chooser` is working as it should (I will edit the code above accordingly). The only problem is that the data isn’t getting saved when I click save.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 1, 2019, 8:51pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/4 "2019-08-01T20:51:18Z")

</div>

> [@pfaffman](#):
>
> {{mini-tag-chooser tags=category.custom\_fields.default\_tag tabindex=“4” categoryId=category.id}}

This line makes me suspicious. I think tags might be taking in an array and as it looks to me, you’re passing a single value(you need to confirm that). If it is being passed as a single value, just make it an array and try.

---

<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: [August 1, 2019, 9:00pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/5 "2019-08-01T21:00:24Z")

</div>

> [@fzngagan](#):
>
> I think tags might be taking in an array and as it looks to me, you’re passing a single value

Well, on the rails side, it’s a `|`-delimited string and I see after I submit that the stuff I’ve entered is getting submitted, e.g., `"custom_fields"=>{"de fault_tag"=>["error", "high-availability", "best-practices"]},` and ` "custom_fields"=>{"de fault_tag"=>["health-checks"]},`

Here’s the whole thing:

```plaintext
Started PUT "/categories/2" for 127.0.0.1 at 2019-08-01 13:56:13 -0700
Processing by CategoriesController#update as */*
  Parameters: {"name"=>"Lounge", "slug"=>"lounge", "color"=>"EEEEEE", "text_color"=>"652D90", "permissions"=>{"trus
t_level_3"=>"1"}, "auto_close_hours"=>"", "auto_close_based_on_last_post"=>"false", "position"=>"3", "email_in"=>""
, "email_in_allow_strangers"=>"false", "mailinglist_mirror"=>"false", "allow_badges"=>"true", "custom_fields"=>{"de
fault_tag"=>["error", "high-availability", "best-practices"]}, "topic_template"=>"", "suppress_from_latest"=>"false
", "all_topics_wiki"=>"false", "allow_global_tags"=>"false", "sort_order"=>"", "sort_ascending"=>"", "topic_feature
d_link_allowed"=>"true", "show_subcategory_list"=>"false", "num_featured_topics"=>"3", "default_view"=>"", "subcate
gory_list_style"=>"rows_with_featured_topics", "default_top_period"=>"all", "minimum_required_tags"=>"0", "navigate
_to_first_post_after_read"=>"false", "search_priority"=>"0", "id"=>"2"}

```

So it looks like the Right Thing is happening on the mysterious side, but that Rails isn’t updating the Category Custom Field. I’ll go back to comparing what I have with another plugin that deals with `CategoryCustomField`s.

Thanks so much, @fzngagan!

EDIT: Hmm. I think the problem is:

```
Unpermitted parameter: :default_tag

```

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 1, 2019, 9:10pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/6 "2019-08-01T21:10:02Z")

</div>

You’ll really enjoy finding the answer to that. Still, if you need help, I would be more than happy to do so. 🙂

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 1, 2019, 9:36pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/7 "2019-08-01T21:36:30Z")

</div>

> [@pfaffman](#):
>
> Unpermitted parameter: :default\_tag

Yes it is. Rails doesn’t know `yet` what to do with this new field. you need to `add_to_serializer` the new field. You can see examples of this in plugins which add custom fields.

---

<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: [August 1, 2019, 9:43pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/8 "2019-08-01T21:43:51Z")

</div>

Well, `plugin.rb` includes:

```plaintext
  Category.register_custom_field_type('default_tag', :list)
  Site.preloaded_category_custom_fields << 'default_tag' if Site.respond_to? :preloaded_category_custom_fields
  add_to_serializer(:basic_category, :default_tag) { object.custom_fields["default_tag"] }

```

It’s over at [GitHub - pfaffman/discourse-topic-default-tag: Allow topics to include default tags · GitHub](https://github.com/pfaffman/discourse-topic-default-tag) if you want to look there.

I’m confused because having

```
   {{input type="list" value=category.custom_fields.default_tag }}

```

worked but

```
{{tag-chooser tags=category.custom_fields.default_tag tabindex="4" categoryId=category.id}}

```

Does not.

EDIT: It seems that using `tag-chooser` my custom field gets sent to Rails as an array and then gets thrown away before it’s processed, but using a `text field` where I create a `|`-delimited string, it works just fine. (I thought that maybe I could fix it with a `before_validation` on the Rails side, but no joy.)

So I guess I need some magic on the Ember side to convert the array to a string before it sends it back?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 3, 2019, 2:07pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/9 "2019-08-03T14:07:17Z")

</div>

I too used `tag-chooser` without params and it fetched all the tags available.

Plus, I think I found the reason why those aren’t getting saved(or even reaching) 😉 on the rails side when using tag-chooser. When you use `input type list` and add a `value` field, that automatically becomes a part of the form.

but the markup generated by the tag chooser is a `div`. Thats why it isn’t added to the form submission.

I am trying to put together a workaround for it. I’ll submit a PR once I get it to work.

---

<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: [August 3, 2019, 7:46pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/10 "2019-08-03T19:46:55Z")

</div>

I talked to @j.jaffeux yesterday and he’s working on making a change.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 3, 2019, 8:05pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/11 "2019-08-03T20:05:54Z")

</div>

> [@pfaffman](#):
>
> So I guess I need some magic on the Ember side to convert the array to a string before it sends it back?

Thats exactly what I am on.

> [@pfaffman](#):
>
> I talked to @joffreyjaffeux yesterday and he’s working on making a change.

Thats great. As a newbie, I am trying to learn more and more and this helps a lot 🙂

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 4, 2019, 7:21pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/12 "2019-08-04T19:21:35Z")

</div>

Hello @pfaffman.

I managed to put together a fix. I wasn’t able to do it from the ember’s side as `tag-chooser` seemed too cool to drop, but I managed it from Rails side.

I wrote a simple function in the CategoriesController and called it `:before_action`, in that I converted the `default_tag` array to a `|` delimited string.

Secondly, I changed `before_update` to `before_commit` as the `custom_fields` seem to be set at that point in execution.

---

<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: [August 4, 2019, 9:56pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/13 "2019-08-04T21:56:33Z")

</div>

Hmm. I thought I had tried that but I’ll have a look when I get to the office. Thanks!

---

<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: [August 5, 2019, 10:06pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/14 "2019-08-05T22:06:31Z")

</div>

> [@fzngagan](#):
>
> I wrote a simple function in the CategoriesController and called it `:before_action` , in that I converted the `default_tag` array to a `|` delimited string.

Aha! That’s what I don’t know how to do. You stick that in `plugin.rb`?

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 6, 2019, 3:34am UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/15 "2019-08-06T03:34:44Z")

</div>

Yes. This did the trick for me.

```
class ::ApplicationController
    def convert_default_tag
      return unless :topic_default_tag_enabled
      puts 'request'
     #Just check whether the field exists to avoid running into errors
      request.params["custom_fields"]["default_tag"] = request.params["custom_fields"]["default_tag"].join('|')
      p request.params["custom_fields"]["default_tag"]
    end
  end
  require 'categories_controller'
  class ::CategoriesController
     before_action :convert_default_tag , only: [:create]
  end
```

I think its possible to have the method `default_tag` in the controller itself, but I couldn’t get it to work that way. As ApplicationController is what every Discourse Controller inherits from, The method is loaded on the onset of extending the class from application controller which does the job.

@merefield  
Is it possible to define a method to be called `:before_action` in the controller itself?

---

<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: [August 6, 2019, 3:37am UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/16 "2019-08-06T03:37:25Z")

</div>

You should put three back ticks before and after the code block.

````
```
 Code goes here
  ```

````

I can’t wait to try it tomorrow! Thanks so much.

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 6, 2019, 3:39am UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/17 "2019-08-06T03:39:45Z")

</div>

Thanks. I thought it was single backtick but was stuck. So I used the plain old html `<pre></pre>` tags to make it work.

---

<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: [August 6, 2019, 6:44am UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/18 "2019-08-06T06:44:29Z")

</div>

> [@fzngagan](#):
>
> @merefield  
> Is it possible to define a method to be called `:before_action` in the controller itself?

Yes, [I believe so](https://guides.rubyonrails.org/action_controller_overview.html#filters)

---

<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: [August 6, 2019, 8:27pm UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/19 "2019-08-06T20:27:34Z")

</div>

That’s a big help. Now I have this:

```ruby
  class ::CategoriesController
    before_action :default_tag_to_string, only: [:create, :update]

    def default_tag_to_string
      puts "CDT: #{params}"

      return unless :topic_default_tag_enabled
      #Just check whether the field exists to avoid running into errors
      request.params["custom_fields"]["default_tag"] = request.params["custom_fields"]["default_tag"].join('|')
      p request.params["custom_fields"]["default_tag"]
    end

  end

```

And now the selected tags get converted to a string before rails tries to update the record and it works as expected.

**NOW** the problem is that I need to figure out where to convert the string on the rails site into the array that we need on the Javascript/Ember side. @j.jaffeux, I can’t remember (or, more likely, didn’t understand) whether the problem you were trying to solve was re-writing this value on the javascript-to-rails side (fixed!) or the rails-to-Javascript side (still confused).

---

<div class="post-metadata">

### Author: ![fzngagan](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/fzngagan/32/259349_2.png) [@fzngagan](https://meta.discourse.org/u/fzngagan)
#### Post date: [August 7, 2019, 2:03am UTC](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656/20 "2019-08-07T02:03:10Z")

</div>

What worked for me is, I changed the `:before_update` to `:before_commit` in your code when assigning tags to the topic.

Secondly, When you open the edit category page, the default tag setting field would require an array in its tags param, in order to display the selected tags.

[Next page](https://meta.discourse.org/t/using-tag-chooser-in-a-plugin/124656.md?page=2)
