ruby-on-rails – Rails上的复选框

Ruby on Rails中制作与某个问题相关的复选框的正确方法是什么?现在我有
<div class="form_row">
    <label for="features[]">Features:</label>
    <br><%= check_Box_tag 'features[]','scenarios' %> Scenarios
    <br><%= check_Box_tag 'features[]','role_profiles' %> Role profiles
    <br><%= check_Box_tag 'features[]','private_messages' %> Private messages
    <br><%= check_Box_tag 'features[]','chatrooms' %> Chatrooms
    <br><%= check_Box_tag 'features[]','forums' %> Forums
    <br><%= check_Box_tag 'features[]','news' %> News
    <br><%= check_Box_tag 'features[]','polls' %> Polls
</div>

我也想能够自动检查先前选择的项目(如果此表单被重新加载).我将如何将参数加载到这些认值?

解决方法

您正在查看以下内容
<div class="form_row">
    <label for="features[]">Features:</label>
    <% [ 'scenarios','role_profiles',...,'polls' ].each do |feature| %>
      <br><%= check_Box_tag 'features[]',feature,(params[:features] || {}).include?(feature) %>
      <%= feature.humanize %>
    <% end %>
</div>

虽然如果您已经拥有Feature模型,功能表和has_many:features关系,您可能希望这样做:

<div class="form_row">
    <label for="feature_ids[]">Features:</label>
    <% for feature in Feature.find(:all) do %>
      <br><%= check_Box_tag 'feature_ids[]',feature.id,@model.feature_ids.include?(feature.id) %>
      <%= feature.name.humanize %>
    <% end %>
</div>

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...