Home#中的NameError显示在ruby中

时间:2015-07-09 15:11:10

标签: ruby-on-rails ruby

我创建了一个名为Bloccit的小型Rails项目。它包含一个关于部分。在About部分中,我定义了一个动作。

<h1>About Bloccit</h1>
<p>Created by: <%= my_name %></p>

同样在application.html.erb部分

<!DOCTYPE html>
<html>
<head>
  <title>Bloccit</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>
    <div class="container">
        <ul class="nav nav-tabs">
            <li><%= link_to "Bloccit", root_path %></li>
            <li><%= link_to "About", about_path %></li>
        </ul>

    <%= yield %>
</div>

</body>
</html>

我已使用<li><%= link_to "About", about_path %></li>标记定义了关于。在我的routes.rb中,我有

 Rails.application.routes.draw do

 resources :posts

 get "welcome/index"

  get 'about' => 'welcome#about'

  root to: 'welcome#index'


  resources :advertisements

end

在我看到路线后,我看到了:

    Prefix Verb   URI Pattern                        Controller#Action
             posts GET    /posts(.:format)                   posts#index
                   POST   /posts(.:format)                   posts#create
          new_post GET    /posts/new(.:format)               posts#new
         edit_post GET    /posts/:id/edit(.:format)          posts#edit
              post GET    /posts/:id(.:format)               posts#show
                   PATCH  /posts/:id(.:format)               posts#update
                   PUT    /posts/:id(.:format)               posts#update
                   DELETE /posts/:id(.:format)               posts#destroy
             about GET    /about(.:format)                   welcome#about
              root GET    /                                  welcome#index
    advertisements GET    /advertisements(.:format)          advertisements#index
                   POST   /advertisements(.:format)          advertisements#create
 new_advertisement GET    /advertisements/new(.:format)      advertisements#new
edit_advertisement GET    /advertisements/:id/edit(.:format) advertisements#edit
     advertisement GET    /advertisements/:id(.:format)      advertisements#show
                   PATCH  /advertisements/:id(.:format)      advertisements#update
                   PUT    /advertisements/:id(.:format)      advertisements#update
                   DELETE /advertisements/:id(.:format)      advertisements#destroy

当我尝试在浏览器中运行此应用程序时,它会给我一个路由错误。 这是我在控制台中收到的错误:

Processing by HomesController#show as HTML
  Rendered homes/show.html.erb within layouts/application (0.1ms)
Completed 500 Internal Server Error in 107ms (ActiveRecord: 0.0ms)

ActionView::Template::Error (undefined local variable or method `about_path' for #<#<Class:0x007f80e226d9e0>:0x007f80e64e42b0>):
    10: <body>
    11: 
    12:         <li><%= link_to "Bloccit", root_path %></li>
    13:         <li><%= link_to "About", about_path %></li>
    14:     </ul>
    15: 
    16: <%= yield %>
  app/views/layouts/application.html.erb:13:in `_app_views_layouts_application_html_erb__31655232790982_70095796178040'


  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.7ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (4.1ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (98.8ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_markup.html.erb (0.9ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.5ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.4ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/console.js.erb within layouts/javascript (102.1ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/main.js.erb within layouts/javascript (0.6ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.8ms)
  Rendered /Users/carlos/.rbenv/versions/2.2.0/lib/ruby/gems/2.2.0/gems/web-console-2.1.3/lib/web_console/templates/index.html.erb (219.3ms)

我正在使用此网址在浏览器中输入:http://localhost:3000/

谢谢, 卡洛斯

1 个答案:

答案 0 :(得分:1)

var allItems = CurrentPage.Children.Where("Visible").ToList(); foreach (var newsYear in allItems) { var yearTotal = allItems.SelectMany(c => c.Children).Count(); foreach (var newsMonth in newsYear.Children) { var monthTotal = newsMonth.Children.Count(); } } 文件中,我会考虑添加此内容;

config/routes.rb

然后,您应该引用get 'about' => 'welcome#about', :as => 'about'