0

I have a collection_radio_buttons on a simple_form form on Ruby on Rails.

Code:

    <%= f.collection_radio_buttons :player_out, @lineup.lineup_players.starting_players, :id, :name, label: "Player to get out?",
        collection_wrapper_tag: :fieldset, collection_wrapper_class: "form-group radio_buttons optional substitution_suggestion_player_out",
        item_wrapper_tag: :div, item_wrapper_class: "form-check" do |player| %>
      <%= player.radio_button %>
      <%= player.label { tag.div(player.text) } %>
    <% end %>

I have two question about it:

1 - My label ("label: 'Player to get out?'") is not working. What I'm doing wrong?

2 - How do I call other methods of the collection? For example to get the player's avatar. :

 <%= player.label { tag.div(image_tag(player.avatar) player.text) } %>

Thanks in advance.

David

David Sousa
  • 71
  • 10

2 Answers2

0

If you want to display an image with radio button you can do something like,

<%= f.collection_radio_buttons :player_out, @lineup.lineup_players.starting_players, :id, :name do |player| %>
  <%= player.radio_button %>
  <%= player.label do %>
    <%= player.text %>
    <%= image_tag player.avatar, height: '100px', width: '100px' %>
  <% end %>
<% end %>

In addition to this, by separately creating the element will allow you to apply a class to them easily.

  • Hi Barkha, thanks for reply, but as I pointed out in the question number 2, I still can't access the methods from "lineup_players" or its scope "starting_player". When I try to access with the following code: <%= player.label do %> <%= player.number %> <% end %> ...I get "undefined method `number' for #". – David Sousa Aug 07 '19 at 16:19
  • Try to use `collection_radio_button` without the `f` object. – Barkha Jariwala Aug 07 '19 at 16:39
  • Still not working: `undefined method 'map' for :id:Symbol Did you mean? tap` – David Sousa Aug 07 '19 at 20:59
0

To push a html code to the label block of the collection_radio_buttons I end up creating a model method that return the html that I wanted to use, and use that method as the text method for the collection.

In my model:

  def html_to_radio_button
    html = ""
    html << "<a class='avatar rounded-circle mr-3' style='background:none !important;'>"
    html << "<img alt='Image placeholder' src='#{ApplicationController.helpers.url_for_player_avatar(self.player)}'>"
    html << "</a>"
    html << "<div class='media-body'>"
    html << "<span class='mb-0 text-sm'>#{self.name}</span><br>"
    html << "<small class='text-muted'>#{self.player.position}</small>"                             
    html << "</div>"
    html.html_safe
  end

On the form:

<%= f.collection_radio_buttons :player_out,lineup.lineup_players.starting_players, :id, :html_to_radio_button, collection_wrapper_tag: :fieldset, collection_wrapper_class: "form-group radio_buttons optional substitution_suggestion_player_out", item_wrapper_tag: :div, item_wrapper_class: "form-check" , label: 'Quem Sai?' do |player| %>
  <%= player.radio_button(class: "form-check-input radio_buttons optional") %>
  <%= player.label(class: "collection_radio_buttons", style: "cursor: pointer;") {content_tag(:div, player.text, class: "media align-items-center")} %>
<% end %>
David Sousa
  • 71
  • 10