用params键轨的情况

时间:2017-02-08 06:05:35

标签: ruby-on-rails case

我在我的代码产品控制器中执行此操作

def index
 if params[:tag]
      #do filter based on tag 
 else
    if params[:category]
      #do filter based on category 
    else
      #default. no filter            
    end
 end
end

它工作正常。如何使用案例实现同样的事情?我只是可以让它工作

case <something?>
 when <tag?> 
    #do filter based on tag 
 when <category?>
    #do filter based on category 
 else
    #default      
end

2 个答案:

答案 0 :(得分:2)

这样做

def index
  case
  when params[:tag]
    # Do filter based on tag 
  when params[:category]
    # Do filter based on category 
  else
    # Default. no filter            
  end
end

答案 1 :(得分:0)

puts case variable
when 1
  # variable value 1 for tag
  #do filter based on tag 
when 2
  # variable value 2 for category
  #do filter based on category 
else
  #default. no filter
end

case语句的简单语法是

case expr0
when expr1, expr2
   stmt1
when expr3, expr4
   stmt2
else
   stmt3
end