今天写程序碰到一个问题,before_filter中的函数总是没有执行,贴出来我的代码:
实验了半天(不会调试ror程序。。汗),发现operate_to_self?返回的总是false,一步步跟踪,发现问题出在这里
通过logger打印出来来看两个值相同,但是比较的结果却是不相同,猜想是两者的类型不同,加上 .to_i 参数,再进行比较,问题终于消失。
ruby 代码
- before_filter :dont_lock_self, :only=>:lock
- before_filter :dont_unlock_self, :only=>:unlock
- before_filter :dont_destroy_self, :only=>:destro
- protected
- def operate_to_self?(user_id)
- if session[:user_id] == user_id
- return true
- end
- return false
- end
- def dont_lock_self
- frag = operate_to_self?(params[:id])
- if frag == true
- flash[:notice] = "You Can not lock Yourself"
- redirect_to :action=>:list
- end
- end
- def dont_unlock_self
- frag = operate_to_self?(params[:id])
- if frag == true
- flash[:notice] = "You Can not unlock Yourself"
- redirect_to :action=>:list
- end
- end
- def dont_destroy_self
- frag = operate_to_self?(params[:id])
- if frag == true
- flash[:notice] = "You can't destroy Yourself"
- redirect_to :action=>:list
- end
- en
ruby 代码
- if session[:user_id] == user_id
ruby 代码
- if session[:user_id].to_i == user_id.to_i