以多选形式(rails)显示先前选择的选项 [英] display previously selected choices in multiple select form (rails)

查看:52
本文介绍了以多选形式(rails)显示先前选择的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经构建了一个多选表单(从 form_for 中),如下所示:

<h4>从来源中排除客户:</h4><%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}), {:include_blank =>false}, {:multiple =>真}%><%= f.error_message_on :excluded_sources %>

这很适合我的需要.唯一的问题是,当我返回显示选项的页面时,我看不到先前选择的内容(即渲染时数据库中已经存在的内容).有没有一种简单的方法可以让 Rails 显示以前选择的内容?我更不想切换到复选框.

在我的匹配配置文件模型中(对应于存储 exclude_sources 的表),我有这个:

序列化:excluded_sources

解决方案

这最终成为了相关的部分:

:selected =>match_profile.send(:excluded_sources)

这里:

<h4>从来源中排除客户:</h4><%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, :selected=>matching_profile.send(:excluded_sources)), {:include_blank =>false}, {:multiple =>真}%><%= f.error_message_on :excluded_sources %>

I've built a multi-select form (from within form_for) like this:

<div class="rounded-block quarter-wide radio-group">
  <h4>Exclude customers from source:</h4>
  <%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}), {:include_blank => false}, {:multiple => true} %>
  <%= f.error_message_on :excluded_sources %>
</div>

this works well for what I need. The only problem is that when i go back to the page that displays the choices, I don't see what was previously selected (i.e. what is already in the DB at time of rendering). Is there an easy way to get rails to display what's previously been selected? I'd MUCH prefer not to switch to check boxes.

in my matching profiles model (corresponding to the table that stores excluded_sources), i have this:

serialize :excluded_sources

解决方案

this ended up being the relevant piece:

:selected => matching_profile.send(:excluded_sources)

here:

<div class="rounded-block quarter-wide radio-group">
<h4>Exclude customers from source:</h4>
<%= f.select :excluded_sources, options_for_select(User.select(:source).group(:source).order(:source).map {|u| [u.source,u.source]}, :selected => matching_profile.send(:excluded_sources)), {:include_blank => false}, {:multiple => true} %>
<%= f.error_message_on :excluded_sources %>

这篇关于以多选形式(rails)显示先前选择的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆