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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10027569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:10:02  来源:igfitidea点击:

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

ruby-on-railsruby-on-rails-3rails-admin

提问by Rahul garg

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

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

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'.

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

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

这在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 :

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

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)

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

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

回答by Benoit B.

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.

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

回答by Evan

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:

Rails Admin 称这些为“虚拟”字段类型。最简单的方法是在你的模型上创建一个方法,然后在你的列表/显示中引用它:

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

回答by SKR

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

回答by Amol Madneuser3501160

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