rails 选择带有多个预选值的标签 [英] rails select tag with multiple values pre selected

查看:27
本文介绍了rails 选择带有多个预选值的标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用多选框.选择框将包含数据库中的所有商店,但将选择用户所属的商店.

I am trying to have a multiple select box. select box will contain all the stores in the DB but the ones that the user belongs to will be selected.

我已经成功了一半.我有一个选择框,其中包含数据库中的所有商店.我无法选择用户所属的那些.

I'm half way there. I got a select box which has all the stores in the database. I'm unable to select the ones that the user belongs to.

我有以下几点:

<%= select_tag 'stores[]', options_for_select(@stores.map {|s| [s.store_name, s.store_id]}, 
:selected => @user.stores.map {|j| [j.store_name, j.store_id]}), :multiple => true, :size => 
10 %>

我有一张地图,上面有用户所属的商店.它在:

I have a map with stores that a user belongs to. it is in:

@user.stores

推荐答案

经过大量的反复试验,以下对我有用:

after a fair amount of trial and error the following worked for me:

<%= select_tag 'stores[]', options_for_select(@stores.map { |s| [s.store_name, s.store_id] }, @user.stores.pluck(:id)), multiple: true, size: 10 %>

这篇关于rails 选择带有多个预选值的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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