undefined local variable current_tab - Rails Views Book Ch 1

时间:2013-07-02 23:54:50

标签: ruby-on-rails partials

我目前正在浏览Rails Views书,第1章 - 布局,并且无法弄清楚为什么我一直收到以下错误消息:

undefined local variable or method `current_tab' for #`
Extracted source (around line #4):
1: <% content_for :main_navigation do %>
2: <nav id="main_nav" role="navigation">
3:  <ul>
4:      <%= nav_tab 'Home', root_path, current: current_tab %></li>
5:      <%= nav_tab 'Creations', creations_path, current: current_tab %></li>
6:      <%= nav_tab 'Campaigns', campaigns_path, current: current_tab %></li>
7:      <%= nav_tab 'Projects', projects_path, current: current_tab %></li>

app/views/layouts/_main_nav.html.erb:4:in `block in _app_views_layouts__main_nav_html_erb__163583644_37720476'
app/views/layouts/_main_nav.html.erb:1:in `_app_views_layouts__main_nav_html_erb__163583644_37720476'
app/helpers/navigation_helper.rb:9:in `currently_at'
app/views/static_pages/home.html.erb:1:in `_app_views_static_pages_home_html_erb___39106558_37242780'

以下是混合中的相关文件:

Application.html.erb

<body>
    <%= render 'layouts/header' %>

_header.html.erb

<header id="page_header" role="banner">
    <nav id="utility">
        <p>
            You are logged in as <strong>Mat Bloody Cauthon</strong>
            <%= link_to "[Your Account]", "#" %> |
            <%= link_to "[Logout]", "#" %>
        </p>
    </nav>
    <%= link_to(image_tag("logo.png", alt: "Artflow", id: "logo"), root_url, title: "Dashboard") %>

    <%= yield :main_navigation %>
</header>

_main_nav.html.erb

<% content_for :main_navigation do %>
<nav id="main_nav" role="navigation">
    <ul>
        <%= nav_tab 'Home', root_path, current: current_tab %></li>
        <%= nav_tab 'Creations', creations_path, current: current_tab %></li>
        <%= nav_tab 'Campaigns', campaigns_path, current: current_tab %></li>
        <%= nav_tab 'Projects', projects_path, current: current_tab %></li>
        <%= nav_tab 'Designers', designers_path, current: current_tab %></li>
    </ul>
</nav>
<% end %>

navigation_helper.rb

module NavigationHelper
    def nav_tab(title, url, options={})
        current_tab = options.delete(:current)
        options[:class] = (current_tab == title) ? 'active' : 'inactive'
        content_tag(:li, link_to(title, url), options)
    end

    def currently_at(tab)
        render 'layouts/main_nav', locals: {current_tab: tab}
    end
end

home.html.erb(root_path)

<%= currently_at 'Home' %>

我真的想要了解Ruby加载东西的顺序,因为我认为它可能是其中的一部分。这是我理解的流程:

  1. 用户导航到根页面(home.html.erb)
  2. Application.html.erb加载,呈现_header.html.erb partial
  3. _header.html.erb通过:main_navigation
  4. 生成_main_nav.html.erb
  5. _main_nav从navigation_helper.rb调用nav_tab()来设置链接
  6. 返回到产生home.html.erb的Application.html.erb
  7. 主页从navigation_helper.rb调用current_at(),它会显示_main_nav.html.erb partial,它应显示当前页面上的导航。
  8. 这种进展是否正确?如果是这样,为什么导轨会在currently_at上窒息,看起来像是current_tab

1 个答案:

答案 0 :(得分:1)

原来问题在于我的currently_at(),行:

render 'layouts/main_nav', locals: {current_tab: tab}

忽略传递的局部变量,因为它使用的是render partial:

的简写版本

将此行更改为:

render partial: 'layouts/main_nav', locals: {current_tab: tab}

解决了这个问题。

这个问题帮助我理解:Render @object and locals vs render :partial

我很欣赏那些看过的人,现在我们都可以为我们的麻烦喝啤酒了。 (至少那是我的计划)