0

当我使用用户可以选择的所有可用对象时,我有两种情况。

第一种情况是用户可以从整个活动列表中选择一个象形图。

第二种情况是用户可以为一个活动选择多个客户端。

在这两种情况下,当我尝试编辑活动时,我都无法让保存的那些被选中/选中。有没有办法做到这一点?

   <div class="pictograms">
      <% for p in Pictogram.all  %>
        <%= radio_button_tag "activity[pictogram_id]", p.id %>
        <%= label_tag(:pictogram_id, image_tag(p.url, :width => "75")) %>
      <% end %>
  </div>
  <div class="clients">
    <% for client in Client.all %>
      <label class="activity">
        <%= check_box_tag "activity[client_ids][]", client.id %>
        <%= client.name %>
      </label>
    <% end %>
  </div>
4

1 回答 1

1

您可以编写对这两种情况都返回 true 和 false 的辅助方法。

view:
<% for p in Pictogram.all  %>
  <%= radio_button_tag "activity[pictogram_id]", p.id, pictogram_is_true?(p) %>
<% end %>

<% for client in Client.all %>
    <%= check_box_tag "activity[client_ids][]", client.id, client_is_true?(client) %>
<% end %>

helper:
def pictogram_is_true?(p)
  // query here and return true or false
end

def client_is_true?(client)
  // query here and return true or false
end
于 2013-10-09T13:37:34.520 回答