【问题标题】:Exclude a partial from certain views从某些视图中排除部分内容
【发布时间】:2016-05-20 19:30:58
【问题描述】:

在我当前的 Rails 应用程序中,我的布局文件夹中有一个部分用于导航侧边栏。除了我的分数控制器的newthankyou 操作之外,我希望这个侧边栏为我的应用程序的每个页面呈现,因为这两个视图将成为 iframe 的一部分。为此,我正在制作另一个名为 iframe.html.erb 的布局,我想将逻辑排除在导航侧边栏之外。

这是我的 application.html.erb 布局文件

<!DOCTYPE html>
<html>
<head>
  <title>NPS</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

    <div id="wrapper">

        <%= render 'shared/navigation' %>
        <%= yield %>
    </div>

</body>
</html>

这是我的 iframe.html.erb 文件

<!DOCTYPE html>
<html>
<head>
  <title>NPS</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

    <div id="wrapper">

        <%= if (current_page?(controller: 'scores', action: 'new') || current_page?(controller: 'scores', action: 'thankyou' )) do %>
            <%= yield %>
        <% end %>
    </div>

</body>
</html>

我最初在我的应用程序布局中只有一个unless 语句,但这在sign-in 页面上不起作用,因为devise 会在devise 文件夹中寻找那些分数视图。我对 html.erb 也不是很好,所以如果只是语法问题,我很抱歉。

编辑:

这是我在设计时遇到的错误

ActionController::UrlGenerationError at /users/sign_in
No route matches {:action=>"new", :controller=>"devise/scores"}

谢谢

【问题讨论】:

    标签: html ruby-on-rails iframe


    【解决方案1】:

    在你的 application.html.erb...

    <%= render 'shared/navigation' unless @disable_nav %>
    

    然后在您拥有两个视图的分数控制器中...

    def new
      @disable_nav = true
      # ...
    end
    
    def thankyou
      @disable_nav = true
      # ...
    end
    

    【讨论】:

      【解决方案2】:

      您不需要两个布局,而且您的语法不正确。删除iframe.html.erb

      有几种方法可以做到这一点。在application.html.erb试试这个:

      <div id="wrapper">
        <% if !current_page?(controller: 'scores', action: 'new') && !current_page?(controller: 'scores', action: 'thankyou') %>
          <%= render 'shared/navigation' %>
        <% end %>
      
        <%= yield %>
      </div>
      

      它应该可以工作。但是,它可以改进。将此逻辑移至您的application_helper.rb

      def should_show_navigation?
        !current_page?(controller: 'scores', action: 'new') && 
          !current_page?(controller: 'scores', action: 'thankyou')
      end
      

      然后,在您的布局文件中 (application.html.erb):

      <div id="wrapper">
        <% if should_show_navigation? %>
          <%= render 'shared/navigation' %>
        <% end %>
      
        <%= yield %>
      </div>
      

      它变得更具可读性。

      【讨论】:

      • 是的,我以前有过这样的事情,问题是当我退出时,由于设计,它给了我一个错误。我将编辑我的帖子以显示错误,以便您更好地理解。由于 application.html.erb 文件中的该语句,设计认为这些控制器和视图位于我的视图的设计子目录中
      • 然后尝试使用current_page?(controller: '/scores', action: 'new'),看看是否可行。
      猜你喜欢
      • 2017-03-28
      • 1970-01-01
      • 2016-07-05
      • 2013-12-10
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      相关资源
      最近更新 更多