0

我正在使用 rails composer 来开发一个快速的基础应用程序。我设置了一个带有主页的 bootstrap-devise-pundit 应用程序(也尝试过 Home、User、About 选项 - 页面在 ../views/pages 中设置为静态)。主页是“访问者”控制器,并作为标准布局查看。Rails 作曲家将 gem high_voltage 用于附加页面(静态),将它们放置在 ../views/pages 文件夹中。我遇到问题的地方是添加我自己的模型/控制器/视图。不是因为他们不工作。我的问题是视图没有使用应用程序页眉/页脚呈现。“访客”页面确实如此。不知道为什么。我可能在这里缺少什么?

例如,

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/

可以查询模型,查看呈现索引(显示和定义/创建的自定义操作)。但是,我没有得到应用程序标头(以及样式表/javascript 包含或页脚。

当然会很感激轻推...

谢谢

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

这是我的应用程序布局。我相信我确实对控制器有看法。但是,它在没有应用程序布局的情况下呈现。

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>

这是访问者控制器和 canopy_aps 控制器

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

为了完整起见,相关视图...

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

如果我将渲染布局:“应用程序”添加到操作,则会渲染应用程序布局,然后是操作视图。而且我必须将其添加到每个操作/方法中以获取要呈现的应用程序布局。在网络上发现的一些帖子建议在控制器顶部使用“布局”应用程序“,这对我不起作用。也值得注意。如果我向访问者控制器添加一个索引操作(它没有定义任何操作,因此呈现默认索引视图),应用程序布局渲染中断并且所有呈现的是访问者索引视图。我想了解这一点。

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
4

0 回答 0