Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional read only on a field

Tags:

rails-admin

It seems like it should be possible to pass in a conditional in place of the true/false that "read_only" is expecting for a field, however that doesn't seem to work. Is there a way to do a conditional read_only on a field based on that field's value?

Things I've tried that don't seem to work:

  • read_only true if self.blah runs but doesn't conditionally make it read_only
  • read_only true if bindings[:object].fieldname.blah gives error NoMethodError: undefined method []' for nil:NilClass at server start
  • read_only true if value.blah gives error NoMethodError: undefined method[]' for nil:NilClass at server start
  • read_only and passing in a lambda instead of just the true/false
like image 246
Chris Avatar asked Jan 26 '26 13:01

Chris


1 Answers

Pass a block:

field :user, :belongs_to_association do
  help "Required. Can't be changed after creation."
  read_only do
    bindings[:object].user_id.present?
  end
end
like image 100
elado Avatar answered Jan 29 '26 11:01

elado