How to create customize view for rails admin?
Asked Answered
S

2

7

Currently, I'm working on a project using rails_admin gem for admin dashboard displaying. But view is auto generate according to Model field.

I want to display my on view in admin dashboard. What is the process of display custom view into rails admin?

Smuts answered 6/9, 2016 at 6:33 Comment(0)
L
9
config.model Utility do
  configure :preview do
    pretty_value do
      util = bindings[:object]
      %{<div class="blah">
        #{util.name} #{util.phone} #{util.logo}
      </div >}
    end
    children_fields [:name, :phone, :logo] # will be used for searching/filtering, first field will be used for sorting
    read_only true # won't be editable in forms (alternatively, hide it in edit section)
  end

  list do
    field :code
    field :priority
    field :preview
  end

  show do
    field :code
    field :priority
    field :preview
  end

  # other sections will show all fields
end

With simple configuration just add a rails_admin block and write a class method to your model then call that method.

app/models/demo.rb
rails_admin do 
  def self.full_name
    "#{first_name} #{last_name}"
  end
end

Now call this method it will return full_name as for example.

Leptophyllous answered 6/9, 2016 at 6:42 Comment(2)
Thanks. Any chance you could modify this to pull in the custom view html from a separate template file, not directly in the config code?Gorki
offcourse, you can do that as long as you have the model object, you can write a method in another template and use that.Leptophyllous
W
3

You can use your own partial:

RailsAdmin.config do |config|
 config.model 'Team' do
  edit do
   field :name do
    partial "my_awesome_partial"
   end
  end
 end
end

The partial should be placed in your applications template folder, such as app/views/rails_admin/main/_my_awesome_partial.html.erb.

The object is available from the partial with form.object. Please check here.

Warnock answered 2/7, 2019 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.