Rails conditional ('if') statements based on controller action

后端 未结 4 1217
别跟我提以往
别跟我提以往 2021-01-31 16:59

There might be a better way to do this, but I\'m trying to make an if statement in rails, based on the current action, in a controller (this will be used in a view).

For

4条回答
  •  有刺的猬
    2021-01-31 17:48

    The params hash that is available in the controller contains :controller and :action keys, which specify the controller and action names of the request.

    Therefore you could say

    if params[:action] == "foo"
      # Show stuff for action 'foo'
    elsif params[:action] == "bar"
      # Show stuff for action 'bar'
    elsif ...
      # etc.
    end
    

提交回复
热议问题