我想解决一个语法问题。我只是试着检查是否加载了控制器/操作,如果是的话,做一些事情,如果不做其他的事情,看起来很简单。这给了我一个错误:
<% if (:controller => 'home', :action => 'index') do %>
<div class="header">
<% else %>
<div class="header-2">
<% end %> 有人能帮我解决这里的语法问题吗?谢谢!
发布于 2015-03-18 11:55:43
您必须修改if子句如下:
<% if controller_name == 'home' && action_name == 'index' %>此外,如果必须不止一次调用这一点,我建议您定义一个助手。
application_helper.rb
def home_index?
controller_name == 'home' && action_name == 'index'
end这样,代码的可读性就会大大提高:
some_view.html.erb
<div class='<%= home_index? ? "foo" : "bar" %>'>发布于 2015-03-18 11:56:25
您可以使用Rails提供的助手来检查这类事情(当前控制器/动作、参数、.):current_page?
<% if current_page?(controller: 'home', action: 'index') %>
<div class="header">
<% else %>
<div class="header-2">
<% end %> 文档:page-3F
使用此助手,您还可以检查特定的参数:
current_page?(controller: 'home', action: 'index', page: '2')发布于 2015-03-18 11:55:30
在视图中,您需要检查controller_name和action_name。尝尝这个
<% if controller_name == 'home' && action_name == 'index' %>
<div class="header">
<% else %>
<div class="header-2">
<% end %> https://stackoverflow.com/questions/29121292
复制相似问题