Spance
Spance

Reputation: 607

Exclude a partial from certain views

in my current Rails app, I have a partial in my layouts folder for a navigation sidebar. I would like this sidebar to render for every page of my app except the new and thankyou actions of my score controller, because those two views are going to be a part of an iframe. To do this, I'm making another layout called iframe.html.erb where I'd like to put the logic to exclude the navigation sidebar.

Here is my application.html.erb layout file

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

And here is my iframe.html.erb file

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

I initially just had an unless statement in my application layout but that wouldn't work on the sign-in page because devise would be looking for those scores views in the devise folder. I'm also not very good with html.erb so I'm sorry if it's just a syntax thing.

Edit:

Here is the error I get with devise

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

Thanks

Upvotes: 0

Views: 1195

Answers (2)

ncrouch25
ncrouch25

Reputation: 270

in your application.html.erb...

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

Then in your score controller where you have you have your two views...

def new
  @disable_nav = true
  # ...
end

def thankyou
  @disable_nav = true
  # ...
end

Upvotes: 3

Diego
Diego

Reputation: 858

You don't need two layouts for that and your syntax is incorrect. Remove iframe.html.erb.

There are several ways to do it. Try this one in 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>

It should just work. However, it can be improved. Move this logic into your application_helper.rb:

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

And then, in your layout file (application.html.erb):

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

  <%= yield %>
</div>

It gets more readable.

Upvotes: 0

Related Questions