Search code examples
ruby-on-railsruby-on-rails-4simple-form

Hover over display related text for simple form association field


I am using Rails and simple form. I have a collection that I am displaying with an association field. For each item in the collection, I want to display a hover over title of an attribute belonging to that item, namely description.

Here is my association:

<%= f.association :user_roles, collection: @roles, as: :check_boxes, label_method: :name, label: false %>

I want to display a hover over text of the description of each role. In my head something like the following should be doable:

<%= f.association :user_roles, collection: @roles, wrapper_html: {title: UserRole.where(id: this_current_items_id).first.description}, as: :check_boxes, label_method: :name, label: false %>

or:

<%= f.association :user_roles, collection: @roles, wrapper_html: {title: :description}, as: :check_boxes, label_method: :name, label: false %>

Neither of these work obviously but in my head a solution like this should be easy. I guess the guys at simple form never thought of this situation.

However, is this possible to do? Or, how can I get the ID of the current item so that I am displaying that items description on hover over?

PS: What I need is a simple form hover_method!


Solution

  • You can easily add other attributes to each item in your collection like so:

    <%= f.association :user_roles, collection: @roles.map { |r| [r.name, r.id, { title: r.description, "data-toggle": "hover" }] }, as: :check_boxes, label: false %>