简体   繁体   English

如何编写条件来测试我在Rails中位于哪个页面?

[英]How do I write a conditional to test which page I'm on in Rails?

What I would like to do is to not show the headder in my rails app if the user is on the home page. 我想做的是,如果用户在主页上,则不要在我的rails应用程序中显示标题。 Right now, the header is rendered in this line in application.html.erb: 现在,标题在application.html.erb的以下行中呈现:

<%= render('layouts/header') %>

What I'd like to do is put a conditional so that this doesn't get rendered if I'm on the home page. 我想做的是设置一个条件,这样如果我在主页上就不会显示该条件。 I think it should be something like: 我认为应该是这样的:

<%= render('layouts/header') if not @homepage %>

where I set @homepage to true in the controller for the homepage. 我在主页控制器中将@homepage设置为true。 Does this approach make sense within the rails framework? 这种方法在Rails框架中有意义吗?

有几种方法可以做到这一点,最简单的方法可能是:

<%= render('layouts/header') unless params[:controller] == 'homepage' %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为 Rails 中的嵌套资源编写此条件语句? - How do I write this conditional statement for a nested resource in rails? Rails:如何引用我当前所在页面的模型? - Rails: How do I reference the model of the page I'm currently on? 如何在Rails中编写条件验证? - How can I write a conditional validation in Rails? 如何在Rails中为集成测试编写帮助程序? - How do I write a helper for an integration test in Rails? 如何为包含语言环境的链接编写Rspec测试? - How Do I Write An Rspec Test for A Link Which Includes a Locale? 使用Mongoid时如何在Rails中测试关联? - How do I test associations in Rails when I'm using Mongoid? RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? 如何在Rails应用程序中将条件重定向写入模型的create方法? - How do I write a conditional redirect into a model's create method, in a Rails app? 如何在Rails 3中编写rspec测试来测试用户在创建后是否使用authlogic进行签名? - How do I write an rspec test in Rails 3 to test if a user is signed with authlogic after being created? 我如何在Rails测试用例中测试我的首页路由 - How do I test my home page route in rails test case
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM