Administrate: Adding Custom Field Type - ChosenRails

Created on 2 Jun 2016  路  4Comments  路  Source: thoughtbot/administrate

I would love to add custom field which uses ChosenRails gem.

I have generated custom field with commad:
rails generate administrate:field chosen
then modified form partial for field:

<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.select field.attribute,
               User.all.map { |u| [u.name, u.id] },
               { include_blank: true },
               { class: 'chosen-select' }
  %>
</div>

and changed attribute type in user_dashboard.rb

  ATTRIBUTE_TYPES = {
    id: Field::Number,
    email: Field::String,
    name: Field::String,
    surname: Field::String,
    manager: Field::ChosenField.with_options(searchable: false),
    created_at: Field::DateTime,
    updated_at: Field::DateTime,
  }.freeze

After this when I try to load my form normal select list appears. I have tried to do the same outside the admin scope and it works fine.

Is there any possibility to make ChosenRails gem work?

Most helpful comment

@Reedian as an engine I don't think administrate will load in your application's javascript.

It does provide a yield(:javascript) though.

So in your field partial you should do

<%= content_for :javascript do %>
    # whatever js you need for chosen rails
<% end %>

All 4 comments

@Reedian as an engine I don't think administrate will load in your application's javascript.

It does provide a yield(:javascript) though.

So in your field partial you should do

<%= content_for :javascript do %>
    # whatever js you need for chosen rails
<% end %>

@Reedian Did the solution suggested by @j-dexx solve your problem? If so, would you consider this issue resolved?

@carlosramireziii yes, it sloved my problem. Thanks for the support.

Glad to hear it! You are very welcome :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gracewashere picture gracewashere  路  3Comments

sedubois picture sedubois  路  4Comments

trandoanhung1991 picture trandoanhung1991  路  3Comments

kwerle picture kwerle  路  4Comments

conwayanderson picture conwayanderson  路  4Comments