little-article-64926
03/04/2026, 8:48 PMminiature-optician-85831
03/04/2026, 8:48 PMlittle-article-64926
03/04/2026, 8:52 PMrb
field :created_by, as: :belongs_to, only_on: [:show], visible: lambda {
resource.authorization.authorize_action(:view_created_by?, raise_exception: false)
}
### Field Formatting
Allows you to restrict the field as needed - also allows using a helper for this, which can help me with this task.
rb
field :created_by, as: :text, only_on: [:show], format_using: lambda {
resource.authorization.authorize_action(:view_created_by?, raise_exception: false) ? link_to(value.name, resources_user_path(value)) : 'Restricted'
}
### Field Block
Allows you to restrict the field as needed.
rb
field :created_by, as: :text, only_on: [:show] do
resource.authorization.authorize_action(:view_created_by?, raise_exception: false) ? link_to(record.created_by.name, resources_user_path(record)) : 'Restricted'
end
However, all these approaches use the field as: :text, while the ideal would be to keep it as: as: :belongs_to, making it cleaner and more readable.
I could use something like:
rb
field :created_by, as: :belongs_to, only_on: [:show] do
return nil unless resource.authorization.authorize_action(:view_created_by?, raise_exception: false)
end
However, nil and 'exists, but you don't have access' are different things; this means, i want to display to the user that he don't have access to that data instead of giving the impression of a blank value for that field.
The question is, is there a better way to do this than this? If so, how?