0

I'm using Bootstrap with a Rails application. I want a div with class .container on the home page and all post show pages. Every other page should have class .container-fluid. I'm trying to accomplish this with javascript in a js.erb file located in app/assets/javascripts. If the path is "/" or "/posts/:id" I will remove class .container-fluid and add class .container to id #wrapper else I will remove class .container and add class .container-fluid. I can't figure out how to do this with all post ids, but I can do it with a specific post id. I've used route helpers in the asset pipeline to get this far. My code below successfully changes the class of post with id 1 to .container. How would I change any post show page to class .container? Any suggestions would be appreciated.

dashboard.js.erb

function wrapperClass() {
  var path = window.location.pathname;
  <% url = Rails101primer::Application.routes.url_helpers %>
  var root_path = "<%= url.root_path %>";
  var post_path = "<%= CGI.unescape url.post_path(1) %>";
  if (path == root_path || path == post_path) {
    jQuery('#wrapper').removeClass('container-fluid');
    jQuery('#wrapper').addClass('container');
  } else {
    jQuery('#wrapper').removeClass('container');
    jQuery('#wrapper').addClass('container-fluid');
    };
};

jQuery(document).ready( function () {
  wrapperClass();
  jQuery(document).on('page:load', function () {
    wrapperClass();
  });
});
Community
  • 1
  • 1
mtminogue
  • 31
  • 1
  • 5
  • why not apply a unique id on home page and then target that id to apply class? – Mandeep Dec 15 '14 at 06:14
  • #wrapper was the unique id I was using to apply the class changes. I've decided to scrap the javascript and move forward by adding .container and .container-fluid to the respective views. It seems not DRY by rails standards, but the javascript is above my skill level. – mtminogue Dec 16 '14 at 01:35

0 Answers0