0

I'm playing with rails composer for a rapid base app. I set up a bootstrap-devise-pundit app with a home page (also tried Home,User,About option - pages are set up in ../views/pages as static). The home page being the "visitors" controller and view as a standard layout would be. Rails composer uses gem high_voltage for additional pages (static) placing them in the ../views/pages folder. Where I'm having issues is adding my own models/controllers/views. Not in that they don't work. My problem is the views aren't rendering with the application header/footer. The "visitor" page does. Not sure why. What might I be missing here?

So for example,

I create a model mymodel.rb in ../app/models
I create a controller mycontroller.rb in ../app/controllers
I create a view index.html.erb in ../app/views/mycontroller/

Model can be queried, view renders index (show and custom actions as defined/created). However, I don't get the application header (along with the stylesheet/javascript includes or footer.

Sure would appreciate a nudge...

Thanks

Default created application layout from rails composer.

root@work:/home/hackerkatt/workspace/canopy_portal# cat app/views/layouts/application.html.erb 
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= content_for?(:title) ? yield(:title) : "Canopy Portal" %></title>
    <meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Canopy Portal" %>">
    <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
    <%= csrf_meta_tags %>
  </head>
  <body>
    <header>
      <%= render 'layouts/navigation' %>
    </header>
    <main role="main">
       <%= render 'layouts/messages' %>
       <%= yield %>
    </main>
  </body>
</html>

Here is a look at my app layout. I believe I do have a view for the controller. It renders, however, without the application layout.

root@work:/home/hackerkatt/workspace/canopy_portal# tree app/

<pre>
  app/
  ├── assets
  │   ├── images
  │   │   ├── bg_header.jpg
  │   │   ├── bullet1.gif
  │   │   ├── bullet2.gif
  │   │   ├── green_indicator.png
  │   │   ├── rails.png
  │   │   ├── red_indicator.png
  │   │   └── yellow_indicator.png
  │   ├── javascripts
  │   │   ├── application.js
  │   │   ├── canopy_aps.js
  │   │   └── progressbar.js
  │   └── stylesheets
  │       ├── application.css.scss
  │       └── framework_and_overrides.css.scss
  ├── controllers
  │   ├── application_controller.rb
  │   ├── canopy_aps_controller.rb
  │   ├── concerns
  │   └── visitors_controller.rb
  ├── helpers
  │   ├── application_helper.rb
  │   ├── layout_helper.rb
  │   └── link_to_function_helper.rb
  ├── mailers
  ├── models
  │   ├── canopy_aps.rb
  │   ├── concerns
  │   ├── gestio.rb
  │   └── user.rb
  ├── services
  │   └── create_admin_service.rb
  └── views
      ├── canopy_aps
      │   ├── index.html.erb
      │   ├── registered_sms.html.erb
      │   └── show.html.erb
      ├── devise
      │   ├── passwords
      │   │   ├── edit.html.erb
      │   │   └── new.html.erb
      │   ├── registrations
      │   │   ├── edit.html.erb
      │   │   └── new.html.erb
      │   └── sessions
      │       └── new.html.erb
      ├── layouts
      │   ├── application.html.erb
      │   ├── _messages.html.erb
      │   ├── _navigation.html.erb
      │   └── _navigation_links.html.erb
      └── visitors
          └── index.html.erb
</pre>

Here is the visitors controller and canopy_aps controller

root@work:/home/hackerkatt/workspace/canopy_portal# cat app/controllers/visitors_controller.rb 
class VisitorsController < ApplicationController
end
root@work:/home/hackerkatt/workspace/canopy_portal# cat app/controllers/canopy_aps_controller.rb
class CanopyApsController < ApplicationController
  before_filter :authenticate_user!

  def initialize
    @apname = ''
  end

  def index
    canopy_aps = Gestio.getAPList()
    @canopy_aps = CanopyAPS.organize(canopy_aps)
  end

  def show
    ip = int2IPOctet(params[:id])
    @apip = ip
    @apname = params[:apname]
    @ap = CanopyAPS.getAPSubscribers(ip)
    @reg_subs = @ap.count
  end

  def registered_sms
    @canopy_aps = Gestio.getAPList()
    @reg_subs = CanopyAPS.getRegSMCounts(@canopy_aps)
    @totals = CanopyAPS.totalsByComp(@reg_subs)
  end
end

For completeness, the related views...

root@work:/home/hackerkatt/workspace/canopy_portal# cat app/views/visitors/index.html.erb 
<h3>Welcome</h3>
root@work:/home/hackerkatt/workspace/canopy_portal# cat app/views/canopy_aps/index.html.erb 
<%= stylesheet_link_tag 'application' %>

<% title "Canopy Aps" %>
<h2>Canopy AP Listing</h2>
<br />

<div class="container-fluid">
<% @canopy_aps.each do |comp,data| %>
  <div style='margin:0 0 0 20px;'>
    <h2><%= comp.camelcase %></h2>
      <table class="table table-striped" style="width:700px;">
        <thead>
          <th>AP</th>
          <th>IP Addr</th>
          <th>Log into SM</th>
        </thead>
        <tbody>
          <% data.each do |ap| %>
            <tr>
              <td><%= ap.host_descr %></td>
              <td><%= int2IPOctet(ap.ip) %></td>
              <td><%= link_to "Get Subscriber List", canopy_ap_path(:id=>ap.ip, :apname=>ap.host_descr) %></td>
            </tr>
          <% end %>
        </tbody>
      </table>
    <div>
  </div>
<% end %>

If I add render layout: "application" to an action the application layout is rendered followed by the actions view. And I have to add this to each action/method to get the application layout to render. Suggested by a few posts found on web was to use 'layout "application"' at the top of the controller, this does not work for me. Also of note. If I add an index action to the visitors controller (which has no actions defined and therefore renders the default index view), the application layout rendering breaks and all that's rendered is the visitors index view. I would like to understand this.

class CanopyApsController < ApplicationController
  before_filter :authenticate_user!

    def initialize
        @apname = ''
    end

  def index
    canopy_aps = Gestio.getAPList()
    @canopy_aps = CanopyAPS.organize(canopy_aps)
    render layout: "application"
  end

  def show
    ip = int2IPOctet(params[:id])
    @apip = ip
    @apname = params[:apname]
    @ap = CanopyAPS.getAPSubscribers(ip)
    @reg_subs = @ap.count
    render layout: "application"
  end    

end
hackerkatt
  • 165
  • 11

0 Answers0