0
votes

My _header.html.erb is

<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 is

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 is

<!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>

Error is

NoMethodError in StatisPages#home

Showing /home/tamal/rails_project/DatabaseManagement_app/app/views/layouts/_header.html.erb where line #25 raised:

undefined method `each' for nil:NilClass

Extracted source (around line #25):

23 24 25 26 27 28

<% for item in @selecteddatabase %>

  • <%= link_to item, currentdatabase_path %>
  • <% $gy = item %> <% end %>

    Trace of template inclusion: app/views/layouts/application.html.erb

    Rails.root: /home/tamal/rails_project/DatabaseManagement_app Application Trace | Framework Trace | Full Trace

    app/views/layouts/_header.html.erb:25:in _app_views_layouts__header_html_erb___324107568__651057508' app/views/layouts/application.html.erb:13:in _app_views_layouts_application_html_erb__249465561_90094870'

    Request

    1

    1 Answers

    0
    votes

    Your controller method header will only be called when your application receives a request that routes to it. However, since you're rendering your _header.html.erb partial in your application layout, it will be included with every request. You are liable to get a NoMethodError every time you refer to @selecteddatabase or @selectedtable when the header method wasn't called.

    If you want the header to work as it is, you need those variables to be defined for every request. One way to do this would be to use 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...
    

    I renamed the method so it wouldn't look like a controller action.


    @selectedtable is assigned conditionally, specifically when $l == 1. You will get this error every time that $l isn't 1.