Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to find input class for json - handling JSON type in Active Admin

I have a JSON type in my model, which is coming from Postgres, the migration looks like:

create_table :people do |t|
  t.string :name
  t.json :links
end

The structure of this JSON object looks like:

{ 
  "facebook" : "u12345",
  "google" : "u54321"
}

And in Active Record I am attempting to display this object so that they can be edited, or a new key/value can be added:

  form do |f|
    f.inputs "Person Details" do
      f.input :name
      f.input :links <- Error here because active admin doesn't recognise JSON type
    end
    f.actions
  end

When I go to edit a person entry from active admin I get "Unable to find input class for json".

I'd like it so that each key in the json structure becomes a label for an input, e.g:

          __________
Facebook | u12345   |
          ‾‾‾‾‾‾‾‾‾‾
          __________
Google   | u54321   |
          ‾‾‾‾‾‾‾‾‾‾
          __________
Twitter  |          |
          ‾‾‾‾‾‾‾‾‾‾

In my active admin form block I want to specify 3/4 pre-defined keys, say facebook, google, twitter, if one of these keys does not exist in the JSON structure coming from the model, it will be displayed as an empty input, allowing the administrator to add a value to that key, and save it back to the database. If the key does exist in the JSON structure, the input will be populated with its value so that it can be edited.

So my question is how can I simply manage/edit a JSON structure from active admin, and represent the data in the above format?

like image 970
StuR Avatar asked Apr 01 '14 13:04

StuR


2 Answers

This works:

  permit_params :name, {:links => [:facebook, :twitter]}

  form do |f|
    f.inputs "Person Details" do
      f.input :name
    end
    f.inputs :name => "Links", :for => :links do |g|
      g.input :facebook, :input_html => { :value => "#{person.links['facebook']}" }
      g.input :twitter, :input_html => { :value => "#{person.links['twitter']}" }
    end
    f.actions
  end

It isn't the nicest solution, but unless someone has a better answer it will have to do.

like image 173
StuR Avatar answered Oct 17 '22 21:10

StuR


There is a gem for that: https://github.com/udacity/activeadmin_json_editor

activeadmin_json_editor lets you edit JSONb fields with a structured field editor. The admins should understand the concept of a hash or array but apart from that, it works out of the box.

like image 30
Ortwin Gentz Avatar answered Oct 17 '22 21:10

Ortwin Gentz