Rails Admin: Conditionally display a field based on a specific object’s value

To conditionally display a field based on a value in the displayed object, you can do the following:

RailsAdmin.config do |config|
  config.model YourObject do 
    edit do 
      field :some_random_attribute do 
        visible do 
          bindings[:object].your_conditional_field=="your_conditional_value"
        end
      end
    end
  end
end

 
In this Rails Admin configuration example, you can see that the YourObject#some_random_attribute field will only display if the YourObject object being displayed satisfies the condition within the “visible” block, otherwise it will not appear.

4 thoughts on “Rails Admin: Conditionally display a field based on a specific object’s value

  1. Claire

    Hi Anthony,

    Thanks for your tutorial. I’m still struggling how my code will work. Please see below syntax. Thanks!

    field :quantity do
    visible do
    bindings[:object].user_id == User.current.id
    end
    end

    Reply
  2. Ali

    Hey I have tried this why I am getting nil when I try to access bindings[:object].your_conditional_field? Please help rails_admin is such a dig in the head thing.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *