2015-06-25 64 views
0

我_header.html.erb是未定義的方法`每個」的零:NilClass中的Rails

<header class="navbar navbar-fixed-top navbar-inverse"> 
     <div class="container"> 
     <%= link_to "Database Management", root_path, id: "logo" %> 
     <nav> 
      <ul class="nav navbar-nav navbar-right"> 
      <li><%= link_to "Home", root_path %></li> 
      <li class="dropdown"> 
      <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
      SelectTable <b class="caret"></b> 
      </a> 
      <ul class="dropdown-menu"> 
      <% if $l == 1 %> 
       <% for items in @selectedtable %>  
       <li><%= link_to items, currenttable_path %> </li> 
        <% $gh = item %> 
       <% end %> 
      <% end %> 
      </ul> 


      <li class="dropdown"> 
      <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
      SelectDatabase <b class="caret"></b> 
      </a> 
      <ul class="dropdown-menu"> 
      <% for item in @selecteddatabase %> 
       <li><%= link_to item, currentdatabase_path %></li> 
        <% $gy = item %> 
      <% end %> 
      </ul> 
      <li><%= link_to "NewTable", newtable_path %></li> 
      <li><%= link_to "NewDatabase", newdatabase_path %></li> 
      <li><%= link_to "DeleteTable", deletetable_path %></li> 
      <li><%= link_to "DeleteDatabase", deletedatabase_path %></li> 
      <li><%= link_to "Connect", connect_path %></li> 
      <li><%= link_to "Disconnect", disconnect_path %></li> 
      <li><%= link_to "Administration", administration_path %></li> 
      <li><%= link_to "Quit", quit_path %></li> 
      </ul> 
     </nav> 
     </div> 
</header> 

application_controller.rb是

require 'mysql2' 
class ApplicationController < ActionController::Base 
    # Prevent CSRF attacks by raising an exception. 
    # For APIs, you may want to use :null_session instead. 
    protect_from_forgery with: :exception 
    $con = Mysql2::Client.new(host: "localhost", username: "root", password: "new_password", database: "mysql") 
    def header 
    rs = $con.query("SHOW databases") 
    i = 0 
    for y in rs 
    i = i + 1 
    end 
    c = 0 
    @selecteddatabase = Array.new(i) 
    for item in rs 
     @selecteddatabase[c] = item 
     c = c + 1 
    end 

    if $l == 1 
     ts = $con.query("SHOW tables") 
     i = 0 
     for y in ts 
     i = i + 1 
     end 
     k = 0 
     @selectedtable = Array.new(i) 
     for items in ts do 
     @selectedtable[k] = items 
     k = k + 1 
     end 
    end 
    end 
end 

application.html.erb是

<!DOCTYPE html> 
<html> 
    <head> 
    <title><%= full_title(yield(:title)) %></title> 
    <%= stylesheet_link_tag 'application', media: 'all', 
              'data-turbolinks-track' => true %> 
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 
    <%= csrf_meta_tags %> 
    <%= render 'layouts/shim' %> 

    </head> 
    <body> 
    <%= render 'layouts/header' %> 
     <div class="container"> 
     <% flash.each do |message_type, message| %> 
     <%= content_tag(:div, message, class: "alert alert-#{message_type}") %> 
     <% end %> 
     <%= yield %> 
     <%= render 'layouts/footer' %> 
     <%= debug(params) if Rails.env.development? %> 
    </div> 
    </body> 
</html> 

錯誤是

NoMethodError在StatisPages#家

顯示 /home/tamal/rails_project/DatabaseManagement_app/app/views/layouts/_header.html.erb 其中線#25提出:

未定義的方法`每個」的零:NilClass

提取的源(圍繞線#25):

在@selecteddatabase%>
  • <%=的link_to逐項

    <%,currentdatabase_path%>

  • <%$ GY =項%> <% 端%>

    模板包含的跟蹤:app/views/layouts/application.html.erb

    Rails.root:/ home/tamal/rails_project/DatabaseManagement_app Application Trace |框架跟蹤|全面跟蹤

    應用程序/視圖/佈局/ _header.html.erb:25:在 _app_views_layouts__header_html_erb___324107568__651057508' app/views/layouts/application.html.erb:13:in _app_views_layouts_application_html_erb__249465561_90094870'

    請求

    回答

    0

    當你的應用程序接收到一個請求你的控制器方法header纔會被調用那路由到它。但是,由於您在應用程序佈局中呈現部分_header.html.erb,因此每請求中將包含。每當您在未調用header方法時參考@selecteddatabase@selectedtable,您都有可能得到NoMethodError

    如果要使標題正常工作,則需要爲每個請求定義這些變量。要做到這一點的方法之一是使用before_filter

    class ApplicationController < ActionController::Base 
        # other stuff... 
    
        before_filter :assign_header_variables 
    
        def assign_header_variables 
        # content of your original header method 
        # ... 
        end 
    
        # more stuff... 
    

    我改名的方法,這樣就不會看起來像一個控制器動作。


    原來的答覆(即錯過了放大):

    @selectedtable被有條件地分配,特別是當$l == 1。每次$l不是1時,您都會收到此錯誤。

    +0

    爲什麼單擊「SelectDatabase」時不顯示下拉菜單。 –

    +0

    如果您的原始問題已解決,並且您遇到新的無關聯問題,則應該提出一個新問題。 –

    相關問題