首页 > 代码库 > Rails多个复选框--check_box_tag

Rails多个复选框--check_box_tag

view

<% @roles.each do |role| %>    <%= check_box_tag roles[], role.id%>    <%= label_tag role.name %>  <% end %>  

生成HTML页面效果

<input type="checkbox" value="1" name="roles[]" id="roles_">  <label for="SuperAdmin">Superadmin</label>    <input type="checkbox" value="2" name="roles[]" id="roles_">  <label for="Teacher">Teacher</label>    <input type="checkbox" value="3" name="roles[]" id="roles_">  <label for="Student">Student</label>  

controller获得信息

"roles"=>["2", "3"]  #params[:roles]