Rails Admin modify list/show view to add new/customized column
Asked Answered
F

4

16

I have setup the rails_admin for the admin interface of my site.

For one of the Model, I want to display an additional column.

say i have name , phone , email, image url, rank etc attributes in my Model(say Student). Then I have to display columns : Name | Rank | Preview(additional column)

In the preview column i want to display some rendered html on the basis of attributes ( email,image,url etc.) for each 'student'.

I have found the way to include a partial for edit/update/create to provide fields/forms as per our partial. But the same implementation of including partial is failing in list/show.

So is there any way I can add the partial to show rendered content, in list/show view for a model...?

Edit: Code added

config.model Utility do
   list do
     field :code
     field :priority
     field :name
     field :url
     field :phone
     field :logo
     field :content
     sort_by :priority
     items_per_page 100
   end
end

This shows up following columns in rails_admin

Code | Priority | Name | Url | Phone | Logo | Content

what i want is Code | Priority | Preview

in which in Preview column i want to show a html rendering content as :

blah.html (just for e.g. html for example , here i want to render in a way it is displayed in one of pages, so it is presentable for admin view too)

<div class="blah">
  <%=util.name%> <%=util.phone%> <%=util.logo%> #usage with proper divs/tags/rendering
</div >
Fredette answered 5/4, 2012 at 11:18 Comment(2)
Please be more concrete. Show some code.Nalda
i have added the code and specified detailed requirement..Fredette
S
27
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

Abstract:

Show/list don't use partials for output. Last overriding point is pretty_value.

Stasiastasis answered 23/4, 2012 at 13:33 Comment(4)
What if "preview" should include some data of some belongs_to model? Let's say Payout.Account.Currency.code ? I used "%{ #{payout.account.currency.code} }" instead of "#{util.name} #{util.phone} #{util.logo}" and it works fine for display, but I can't get sorting on 'code' working. How to make children_fields work on associated model fields?Catastrophism
This will work, but you need to add a method in the Utility model, preview, returning any value you want.Entrammel
I had to add .html_safe to my output string to make it show up properly.Summit
Actually you can use a partial, ex: pretty_value { bindings[:view].render partial: 'my_partial' } Porfirioporgy
D
7

Rails Admin calls these "virtual" field types. The easiest way is to make a method on your model, and then refer to it it in your list / show:

class ModelName < ActiveRecord::Base

  def invite_link
    %{<a href="http://site.com/#{self.uid}">invite link</a>}.html_safe
  end

  rails_admin do
    configure :invite_link do
        visible false # so it's not on new/edit 
    end

    list do
      field :name
      field :invite_link
    end

    show do
      field :name
      field :invite_link
    end
  end
end
Driest answered 9/5, 2013 at 23:52 Comment(1)
Even though this would work, it's better to avoid putting this view logic into the model.Wiggly
E
-1
class Utility < ActiveRecord::Base 
  def preview
    name  
  end
end

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
Entrammel answered 18/12, 2013 at 7:39 Comment(1)
From the preview method, return any non null value.Entrammel
T
-1
class ModelName < ActiveRecord::Base
  rails_admin do
    list do
      field :job_title
      field :required_experiance
      field :salary 
      field :technical_skills
      field :non_technical_skills     
    end

    create do
      field :job_title, :enum do
        help 'Please select Job Title'
        enum do
          ['Business Analyst', 'Trainee Business Analyst', 'Mobile/Web Developer',
           'iOS Developer', 'Graphic Designer', 'System Administrator', 'Content Writer']
        end
      end

      field :job_type do
        help 'e.g. Developer, Management'
      end

      field :undergraduate_degree, :enum do
        help 'Please select UG Degree'
        enum do
          [ 'BE', 'BCA', 'B.Tech','BCs', 'BSc', 'BBA', 'BA', 'BCom', 'BSL']
        end 
      end

      field :postgraduate_degree, :enum do
        help 'Please select PG Degree'
        enum do
          [ 'ME', 'MCA', 'M.Tech', 'MCs', 'MSc', 'MBA', 'MCM', 'MMM', 'MA', 'MCom']
        end 
      end

      field :required_experiance, :enum do
        help 'Please select Year'
        enum do
          [ 'Select Year', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
      end 
    end
  end
end
Thaxter answered 5/4, 2014 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.