# Campo personalizzato non funzionante

**URL:** https://meta.discourse.org/t/custom-field-not-working/89983
**Category:** Development
**Created:** [15 Giugno 2018, 4:52pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983 "2018-06-15T16:52:29Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [15 Giugno 2018, 4:52pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/1 "2018-06-15T16:52:29Z")

</div>

Hi All,

I am fairly new to discourse and I am attempting to add a customs field but I am not able to,

Could someone please help me.

My code

```plaintext
after_initialize do

  module ::Foo
  	 PLUGIN_NAME ||= -"discourse-myapi"
     STORE_NAME ||= "replies".freeze

    class Engine < ::Rails::Engine
      engine_name PLUGIN_NAME
      isolate_namespace Foo

    end
  end

  require_dependency "application_controller"

  class Foo::Example
  	class << self

        def bump(id)
           cf_time = Time.zone.now 	
          DistributedMutex.synchronize("#{Foo::PLUGIN_NAME}-#{cf_time.iso8601}") do
          	user = User.find(id)
            user.custom_fields['signature_url'] = cf_time
            user.save!
            return user
          end
        end

  	end	
  end

  class Foo::BarController < ::ApplicationController

  	requires_plugin Foo::PLUGIN_NAME

    def custom_field_test
      begin
        user = Foo::Example.bump(1)
        render json: { user: user }
      rescue StandardError => e
        render_json_error e.message
      end
    end

  end

  Foo::Engine.routes.draw do'
    get '/custom_field_test' => 'bar#custom_field_test'
  end

  Discourse::Application.routes.append do
    mount ::Foo::Engine, at: '/myapiclass'
  end

end

```

Thanks

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [16 Giugno 2018, 12:15pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/2 "2018-06-16T12:15:44Z")

</div>

I am going to answer my own question here, since I kind of understand where I was making mistake in terms of my thinking process, and also a benefit of getting feedback from the rest of the team here, as well as for the benefit of new comers to custom\_fields functionality in Discourse.

Basically I was making a mistake in thinking without investigation that after I create a custom field on User object by doing

**user = User.find(1)**  
**user.custom\_fields[“test\_field”] = " X X X X"**  
**user.save**

I was expecting the custom field to be created on the user object i.e. to become

**custome\_field\_test\_field**

and I was doing:

**user.custom\_fields\_test\_field**

But after investigating deeply, I found out that, **custom\_field** is actually an object

**user.custom\_fields[“test\_field”] = { }**

and when I do

**user.custom\_fields[“test\_field”] = " X X X X"**

it appends it to the hash/object

Therefore it becomes

**user.custom\_fields = {**  
**“test\_field”] = " X X X X"**  
**}**

---

<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: [16 Giugno 2018, 12:56pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/3 "2018-06-16T12:56:01Z")

</div>

You also want to add a name to your custom field. The importers all use custom fields, so you could look there for examples.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [16 Giugno 2018, 1:27pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/4 "2018-06-16T13:27:04Z")

</div>

Hi @pfaffman

Thanks for feedback, but I am not sure if I am following it, could you please give example.

And also on the rails console if I do

```
[10] pry(main)&gt; user = **User**.find( **1** )
[11] pry(main)&gt; pp user.custom_fields 

(2.5ms) SELECT "user_custom_fields"."name", "user_custom_fields"."value" FROM "user_custom_fields" WHERE "user_custom_fields"."user_id" = 1 ORDER BY id asc

{"xxxxxxxxxxxxxxxx"=&gt;"2018-06-16 12:23:39 UTC",

 "test_two"=&gt;"1234567",

 "fountain_cake"=&gt;"2018-06-16 13:16:25 UTC"}

=&gt; **{**" **xxxxxxxxxxxxxxxx**" **=&gt;**" **2018-06-16 12:23:39 UTC**" **,**" **test_two**" **=&gt;**" **1234567**" **,**" **fountain_cake**" **=&gt;**" **2018-06-16 13:16:25 UTC**" **}**

```

I can see the custom fields I added,

But on the plugin:

```
def custom_field_test
  begin
  	 cf_time = Time.zone.now 	
     user = User.find(1)
     user.custom_fields['fountain_cake'] = cf_time
     user.save!
    user_after_update = User.find(1)
    json = serialize_data(user_after_update, UserSerializer, root: false)
    render json: { user: json }
  rescue StandardError => e
    render_json_error e.message
  end
end

```

It returns the custom\_fields as empty object, any thought ?

---

<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: [16 Giugno 2018, 1:44pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/5 "2018-06-16T13:44:21Z")

</div>

Look in scripts/import\_scripts at any importer to see how they create custom fields.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [16 Giugno 2018, 4:09pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/6 "2018-06-16T16:09:20Z")

</div>

@pfaffman thanks for let me know about the necessary of adding importers for custom field I am looking into them,

On the second part of the question, do you know if Redis caches requests, when you call the DB from rails application ? because at the moment I am two distinct results, calling via the controller and directly to the database via rails console.

Thanks

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [18 Giugno 2018, 10:21am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/7 "2018-06-18T10:21:26Z")

</div>

Anybody know how I can make the “custom\_fields” on User visible ? at the moment when I fetch user object I am getting the custom\_fields property is empty object/hash - {}.

But if I do user.custom\_fields, I can see all the custom fields, however as part of the User object is empty.

Please refer to the code snippet above for more details.

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [18 Giugno 2018, 11:35am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/8 "2018-06-18T11:35:33Z")

</div>

For that you have to whitelist those custom fields in either `public user custom fields` or `staff user custom fields` site setting according to your need.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [18 Giugno 2018, 11:43am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/9 "2018-06-18T11:43:39Z")

</div>

@vinothkannans Thanks for reply,

I am new to discourse and I am not sure, when you say - public user custom fields - if you are referring to the UI part of discourse, on JS or the actual Ruby code. could you please elaborate, or point me to an example.

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [18 Giugno 2018, 11:51am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/10 "2018-06-18T11:51:16Z")

</div>

I mean you have to add that custom field’s name in admin site setting at the URL `/admin/site_settings/category/all_results?filter=public%20user%20custom%20fields` like below

![Screenshot%20from%202018-06-18%2017-19-35](https://global.discourse-cdn.com/meta/original/3X/8/f/8fe3c3bb8d412d270001d22581b3182ad9c84a17.png)

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [18 Giugno 2018, 11:57am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/11 "2018-06-18T11:57:46Z")

</div>

@vinothkannansThanks so much,

I can see the field returning as part of the API.

Although I would like to know the wiring under the hood as to why you need to whitelist it ?

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [18 Giugno 2018, 12:09pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/12 "2018-06-18T12:09:18Z")

</div>

Custom fields may contain sensitive user data. We shouldn’t expose it by default.

---

<div class="post-metadata">

### Author: ![PoojaPatel](https://avatars.discourse-cdn.com/v4/letter/p/edb3f5/32.png) [@PoojaPatel](https://meta.discourse.org/u/PoojaPatel)
#### Post date: [7 Luglio 2018, 8:25am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/13 "2018-07-07T08:25:06Z")

</div>

is this features work for requirement, I want to see all users in two different categories, “supplier” or “customer”

If i add into customize → User Fields → Filed Type → Dropdown \> Add Field Name

> Require tick from below two  
> **Options**  
> Supplier  
> Customer

Can anyone help me for that, Where i can see this report? It is come with **\>** users List

---

<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: [8 Luglio 2018, 5:47am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/14 "2018-07-08T05:47:43Z")

</div>

You probably need the [data Explorer](https://meta.discourse.org/t/32566?silent=true) plugin to get a report.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [24 Luglio 2018, 4:38pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/15 "2018-07-24T16:38:53Z")

</div>

@vinothkannans . Thanks for replying.

And I have another question about custom fields, I am not able to see the custom\_field on Posts object, nor I can whitelist it on the ui admin page, can you help with this,

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [24 Luglio 2018, 5:09pm UTC](https://meta.discourse.org/t/custom-field-not-working/89983/16 "2018-07-24T17:09:55Z")

</div>

You should manually add it from a plugin using `add_to_serializer(:post, ...` method.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [25 Luglio 2018, 10:20am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/17 "2018-07-25T10:20:01Z")

</div>

@vinothkannans Thanks for suggestion, and it’s working.

One more thing I would like to ask you is, I am trying to set an object as in a JSON object into the custom field, However, when I se it, is setting it as a string with character escapes, is there a way to handle this.

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [25 Luglio 2018, 10:48am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/18 "2018-07-25T10:48:09Z")

</div>

```plaintext
JSON.parse(post.custom_fields["FIELD_NAME"])

```

You can use above command to convert the string to JSON object.

---

<div class="post-metadata">

### Author: ![Sanjay1](https://avatars.discourse-cdn.com/v4/letter/s/5f8ce5/32.png) [@Sanjay1](https://meta.discourse.org/u/Sanjay1)
#### Post date: [25 Luglio 2018, 11:34am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/19 "2018-07-25T11:34:08Z")

</div>

Thanks @vinothkannans

Last question, do you know if is possible in Discourse, to intercept a response from POST request, modify the body, e.g. add a custom field before sending to the client ?

Thanks

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [25 Luglio 2018, 11:44am UTC](https://meta.discourse.org/t/custom-field-not-working/89983/20 "2018-07-25T11:44:21Z")

</div>

Yes, it’s possible. You should go through to the available methods in [plugin/instance.rb](https://github.com/discourse/discourse/blob/master/lib/plugin/instance.rb) file. You can also check existing plugin codes to learn about how they handling different situations. And go through [Developing Discourse Plugins - Part 1 - Create a basic plugin](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-1/30515).

Edit: You can also monkey patch controller methods.

[Pagina seguente](https://meta.discourse.org/t/custom-field-not-working/89983.md?page=2)
