Vlad Balanescu
Vlad Balanescu

Reputation: 674

Assign value to rails_admin list field

I am using rails_admin gem and instead of the actual id field from the database I want to use just a current number, so that the admin won't get to see what the actual id is in my db. So far I tried:

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

But this displays just my db id. Any suggestions?

Upvotes: 0

Views: 829

Answers (2)

Vlad Balanescu
Vlad Balanescu

Reputation: 674

[Answer]: As long as the db does reset it's id value (e.g: MySql), you can do this:

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

If the database keeps incrementing that value, than the current user has to be passed as a binding and the id can be easily tweaked in the main list view of rails_admin

Upvotes: 0

Satendra
Satendra

Reputation: 6865

You can do following for this purpose.

  1. hide field

    list do
      configure :id do
         hide
      end
    
      include_all_fields # all other default fields will be added after, conveniently
    end
    
  2. exclude field

    list do
      exclude_fields :id
    end
    
  3. format field value

    list do
      field :id do
         formatted_value do # used in form views
           value++
         end
      end
    
      include_all_fields # all other default fields will be added after, conveniently
    end
    

for more configuration visit https://github.com/sferik/rails_admin/wiki/Fields

Upvotes: 1

Related Questions