rails中的select下拉列表使用

xiaoxiao2024-03-24  26

最近在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]....endif "tea"==user_type[0]....end 结论,我猜测 if "stu"==user_type,无法实现应该是由于user_type是个数组的原因吧。 但是,在下拉列表中选中的值,只有一个值,为何要用数组呢;而且将user_type存入 session,和显示session值均未用到数组形式。作为标记一下。
转载请注明原文地址: https://www.6miu.com/read-5014497.html

最新回复(0)