Rails 中的条件部分取决于上下文页面? [英] Conditional in Rails partial depending on the context page?

查看:42
本文介绍了Rails 中的条件部分取决于上下文页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我是否可以根据出现的上下文在部分中以不同的方式执行/显示某些内容.

I was wondering if I could execute / display some stuff differently in partials depending on the context in which it appears.

例如,我有一个 _user_info 部分出现在侧边栏和用户页面中,我想在第二种情况下显示一些额外的信息.我该如何表达这种条件?

For example, I have a _user_info partial that appears in a sidebar, and also in the user page, and I want to display some extra info in the second case. How can I express that kind of conditions?

推荐答案

你要的是这样的:

if params[:controller].eql?('users')
  view code

但是,我会将部分拆分为两个单独的部分,并从您需要的任何视图中显示不同的部分.

However I would split the partial into two separate partials and display the distinct parts from whichever view you need.

这篇关于Rails 中的条件部分取决于上下文页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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