重置rails_admin字段值

时间:2016-11-16 09:25:22

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

我想在我的信息中心的列表视图中执行当前编号,而不是正确的数据库ID字段。为此,我使用formatted_value中的rails_admin。唯一的问题是,我希望在呈现表单后重置该值,因为在我重新加载页面之后,该id保持递增,并且永远不会返回0.如何重置字段中的formatted_value rails admin。到目前为止,我尝试了这个没有太多运气:

config/initializers/rails_admin.rb

  currentId = 0
  # Fields in Projects list
  config.model 'Project' do
    list do
      field :id do
        formatted_value do
          currentId += 1
        end
      end
      field :year
      field :title
      field :intro
      field :description
      field :confidential
      field :star
      field :image
    end
    currentId = 0

1 个答案:

答案 0 :(得分:0)

[解决]

我将field :id留在了初始化程序中并覆盖了app/views/rails_admin/main/index.html.haml with

%th.last.shrink
  %tbody
    - currentId = 1
    - @objects.each do |object|
      %tr{class: "#{@abstract_model.param_key}_row"}
        %td
          = check_box_tag "bulk_ids[]", object.id, false
        - if @other_left_link ||= other_left && index_path(params.except('set').merge(params[:set].to_i != 1 ? {set: (params[:set].to_i - 1)} : {}))
          %td.other.left= link_to "...", @other_left_link, class: 'pjax'
        - properties.map{ |property| property.bind(:object, object) }.each do |property|
          - if property.name.to_s == "id"
            - value = currentId
            - currentId += 1
          - else
            - value = property.pretty_value