通过视图中的多个复选框选择循环

时间:2016-05-25 10:53:49

标签: ruby-on-rails forms

为了显示多个图标,用户在视图中创建新帖子时会进行检查。

new.html.erb

<%= check_box_tag 'post[tech_icon][]', 'fa-diamond', checked('fa-diamond'), id: "message_area" %>
<%= check_box_tag 'post[tech_icon][]', 'fa-html5', checked('fa-html5'), id: "message_area" %>
<%= check_box_tag 'post[tech_icon][]', 'fa-css3', checked('fa-css3'), id: "message_area" %>

show.html.view

<% current_user.posts.each do |post| %>
  * bellow only renders one checked selections (the last one checked)
  <span><i class="fa <%= post.tech_icon %>"></i></span>

  * bellow just renders the selected option text on the screen
  <%= post.tech_icon %> 
<% end %>

***如果你有更好的头衔,我可以告诉我。

enter image description here

enter image description here

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

只需替换它:

  <span><i class="fa <%= post.tech_icon %>"></i></span>

<% post.tech_icon.split(",").each do |icon| %>
    <span><i class="fa <%= icon %>"></i></span>
 <% end unless post.tech_icon.blank? %>