ruby-on-railsrails-admin

RailsAdmin - hidden form fields and labels


Using RailsAdmin. I have a Post model and a User model. Each post belongs to a user.

I use the following code to get RailsAdmin to handle the association for me and automatically set the user_id when a Post is created:

  config.model Post do 
    edit do
      field :user_id do
        # use a form_field instead of the default drop-down
        partial :form_field
        # hide the input
        view_helper :hidden_field
        # set the value to current_user.id
        def value
          bindings[:view]._current_user.id
        end
      end 
    end
  end

This code works, it sets the user_id to that of the current_user and it also hides the form_field (the html input) from view so that user is not even aware that it is being set on their behalf.

There is one small problem though. Whilst I'm able to hide the form_field, I can't hide it's associated label (i.e. the label that reads "User" which appears next to the input) - which means my users see this:

enter image description here

As you can see, there is a label "User" with an empty space next to it, and the word "Required" underneath.

Is there anyway to hide an input's associated label too (and not just the input itself) so that it's not confusing to the user? Is there something wrong with my code?

Thanks in advance


Solution

  • I think I've found a working solution:

      field :user_id do
        view_helper :hidden_field
    
        # I added these next two lines to solve this
        label ""
        help ""
    
        partial :form_field
        def value
          bindings[:view]._current_user.id
        end
      end 
    

    Not ideal, but it works