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.

ABOUT THE AUTHOR

Anthony Leon

Author has no description.

4 COMMENTS

  1. Juan Pabo
     - Reply

    Small fix.

    it should be:
    field :some_random_attribute do

    instead of:
    field :some_random_attribute

    • Anthony Leon
       - Reply

      Thanks Juan – the syntax has been updated.

  2. Claire
     - Reply

    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

  3. Ali
     - Reply

    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.

Leave a comment

Your email is never published or shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>