`
mqh444
  • 浏览: 9766 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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值均未用到数组形式。作为标记一下。
分享到:
评论
1 楼 真无名 2009-03-26  
因为multi select的tag一样是<select></select>
发表评论

文章已被作者锁定,不允许评论。

相关推荐

Global site tag (gtag.js) - Google Analytics