Rails管理员修改列表/显示视图以添加新/自定义列

时间:2012-04-05 11:18:45

标签: ruby-on-rails ruby-on-rails-3 rails-admin

我已经为我网站的管理界面设置了rails_admin。

对于其中一个模型,我想显示一个额外的列。

说我的模型中有姓名,电话,电子邮件,图片网址,排名等属性(比如学生)。 然后我必须显示列:名称|排名|预览(附加栏目)

在预览栏中,我想根据每个“学生”的属性(电子邮件,图片,网址等)显示一些渲染的html。

我找到了包含部分编辑/更新/创建的方式,以根据我们的部分提供字段/表单。但是包含partial的相同实现在list / show中失败了。

那么有什么方法可以添加部分显示渲染内容,在模型的列表/显示视图中......?

编辑:已添加代码

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

这会在 rails_admin

中显示以下列

代码|优先|名称|网址|电话|标志|内容

我想要的是什么 代码|优先|预览

其中在“预览”列中我想将html呈现内容显示为:

blah.html(仅用于例如html,例如,我想以某种方式呈现它在一个页面中显示,因此它也适用于管理员视图)

<div class="blah">
  <%=util.name%> <%=util.phone%> <%=util.logo%> #usage with proper divs/tags/rendering
</div >

4 个答案:

答案 0 :(得分:23)

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

摘要:

显示/列表不使用部分输出。最后一个重点是pretty_value

答案 1 :(得分:7)

Rails管理员称这些为&#34;虚拟&#34;字段类型。最简单的方法是在模型上创建一个方法,然后在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

答案 2 :(得分:-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

答案 3 :(得分:-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