简体   繁体   English

如何在Simple_Form collection_radio_buttons上使用集合的其他方法?

[英]How to user other methods of the collection on a Simple_Form collection_radio_buttons?

I have a collection_radio_buttons on a simple_form form on Ruby on Rails. 我在Ruby on Rails的simple_form表单上有一个collection_radio_buttons。

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. 1-我的标签(“标签:'玩家要出去吗?'”)无效。 What I'm doing wrong? 我做错了什么?

2 - How do I call other methods of the collection? 2-如何调用集合的其他方法? For example to get the player's avatar. 例如获取玩家的头像。 :

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

Thanks in advance. 提前致谢。

David 大卫

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. 除此之外,通过单独创建元素,您可以轻松地将类应用于它们。

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. 要将html代码推送到collection_radio_buttons的标签块中,我最终创建了一个模型方法,该方法返回想要使用的html,并将该方法用作集合的文本方法。

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 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM