Best way to conditionally format fields in different views #1963
-
I'm using Avo in my Rails project and I have a requirement where I want to conditionally format fields based on the view they are in. Specifically, I want to use format_using to apply formatting only in the index and show views, and not in the edit or new views. I came up with a solution using conditional logic within format_using to check the view variable: field :volume, as: :number do |volume, view|
format_using do
if [:index, :show].include?(view)
number_with_delimiter(volume, delimiter: ' ')
else
volume
end
end
end Is this the best way to achieve this or does Avo have a more idiomatic way to conditionally format fields based on the view? Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @vishthemenon Yes, that's the correct way to do it. |
Beta Was this translation helpful? Give feedback.
Hey @vishthemenon
Apologies for the looong delay.
Yes, that's the correct way to do it.
The only Avo 3 addition is that
view
is a smart object and you can doif view.display?
.