Custom Field not working


(Jay Pfaffman) #3

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


(KLm) #4

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)> user = **User** .find( **1** )
[11] pry(main)> 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"=>"2018-06-16 12:23:39 UTC",

 "test_two"=>"1234567",

 "fountain_cake"=>"2018-06-16 13:16:25 UTC"}

=> **{ **"** xxxxxxxxxxxxxxxx **"** => **"** 2018-06-16 12:23:39 UTC **"** , **"** test_two **"** => **"** 1234567 **"** , **"** fountain_cake **"** => **"** 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 ?


(Jay Pfaffman) #5

Look in scripts/import_scripts at any importer to see how they create custom fields.


(KLm) #6

@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


(KLm) #7

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


(Vinoth Kannan) #8

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.


(KLm) #9

@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


(Vinoth Kannan) #10

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


(KLm) #11

@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


(Vinoth Kannan) #12

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


(Pooja Patel) #13

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


(Jay Pfaffman) #14

You probably need the data Explorer plugin to get a report.


(KLm) #15

@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


(Vinoth Kannan) #16

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


(KLm) #17

@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


(Vinoth Kannan) #18
JSON.parse(post.custom_fields["FIELD_NAME"])

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


(KLm) #19

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


(Vinoth Kannan) #20

Yes, it’s possible. You should go through to the available methods in plugin/instance.rb file. You can also check existing plugin codes to learn about how they handling different situations. And go through Beginner’s Guide to Creating Discourse Plugins - Part 1.

Edit: You can also monkey patch controller methods.


(KLm) #21

@vinothkannans Again thanks for suggestion,

I have successfully added field.

I have one issue though the add_to_serializer is loaded before a HTTP request comes in, therefore if I set the value of the custom field with params[“value_to_field”] from the params Hash, it fails for obvious reason, the fact that it add_to_serializer does not have access to params, is there a way to circumvent this limitation.

Thanks


(Vinoth Kannan) #22

You can get params only in controllers.

You have to understand the differences between serializers and controllers. Also read other Rails essentials.