rails中的select下拉列表使用

最近在rails开发中有用到下拉列表的元素,用来描述和区分用户的类型.
类型包括
学生,值为stu,
教师,值为tea, 该信息保存在USER_TYPE中,

USER_TYPES = [
[ "学生", "stu" ],
[ "教师", "tea" ],
].freeze

view层代码为:

<p>
<label for="user_type">类型:</label>
<%=
options = [["请选择类型", ""]] + Student::USER_TYPES
select("user_type",params[:user_type] , options)
%>
</p>

然后打算在Controller里面,获得user_type的值,然后通过条件判断用户类型,从而进行不同的处理。
开始的代码为:

......
user_type = params[:user_type]
if "stu"==user_type

....
end
if "tea"==user_type

....
end

但if判断部分"stu"==user_type总是无法实现。
我把user_type存入session[:user_type]中,然后输出session值,是stu或者tea,是正确的。
最后我的做法是把if判断部分,user_type写为数组形式user_type[0],则程序实现。
即:

......
user_type = params[:user_type]
if "stu"==user_type[0]
....
end
if "tea"==user_type[0]
....
end

结论,我猜测 if "stu"==user_type,无法实现应该是由于user_type是个数组的原因吧。
但是,在下拉列表中选中的值,只有一个值,为何要用数组呢;而且将user_type存入 session,和显示session值均未用到数组形式。作为标记一下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值