We've moved discussions to Discord

How to prepopulate account id in the administrate dashboard when creating a new item?

Georg Törnqvist
Hi!

I'm just getting started with administrate and I have a question.

When creating a new item of a model, let's say "Bus", in the dashboard, how can I to populate the account attribute of a new Bus based on the currently used account?

I've tried to add a custom field type AccountField, which does work as expected in the _index.htlm.erb and _show.html.erb. But the _form.htlm.erb doesn't work doesn't seem to work..

All the best,
Georg

_form.html.erb

<div class="field-unit__label">
  <%= f.label field.attribute %>
</div>
<div class="field-unit__field">
  <%= f.text_field field.attribute %>
</div>


AccountField

require "administrate/field/base"
include CurrentHelper

class AccountField < Administrate::Field::Base
  def account
    puts data
    if data.current_account
      data.current_account.id
    else
      CurrentHelper.current_account.id
    end
  end
end


Bus model

class Bus < ApplicationRecord
  acts_as_tenant :account
  belongs_to :account
  has_one driver
...

Georg Törnqvist
Hi!

Just letting you know that I'll revisit this topic at a later time.

Apparently, madmin is gonna replace administrate. 
Notifications
You’re not receiving notifications from this thread.