基于控制器动作的 Rails 条件 ('if') 语句 [英] Rails conditional ('if') statements based on controller action

查看:46
本文介绍了基于控制器动作的 Rails 条件 ('if') 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能有更好的方法来执行此操作,但我正在尝试根据当前操作在控制器中(这将在视图中使用)在 rails 中创建一个 if 语句.

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).

例如,如果它是编辑页面或显示页面等.我想要不同的样式 - 是否有可以指定这一点的 if 语句?

For example, if its the edit page, or the show page, etc. I'd like a different style for something - is there an if statement that can specify this?

(我需要一个 if 语句,因为它用于部分页面,在多个页面上).

(I need an if statement, because its used in a partial, on multiple pages).

谢谢!

艾略特

推荐答案

控制器中可用的 params 哈希包含 :controller:action 键,指定请求的控制器和动作名称.

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

所以你可以说

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

这篇关于基于控制器动作的 Rails 条件 ('if') 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆