Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails render partial without entire html layout

Ok so I have a problem with rails and rendering partials. I have a layout called profile and inside of the profile layout I have all my js, stylesheets, etc included.

<html>
<head>
  <title>Profile</title>
    <%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" %>
    <%= javascript_include_tag "https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.10/jquery-ui.min.js" %>
    <%= javascript_include_tag "application" %>
    <%= stylesheet_link_tag "main" %>
    <%= stylesheet_link_tag "reset" %>
    <%= csrf_meta_tag %>
</head>

<body>
 <%= yield %>
</body>
</html>

Inside of the yield tag(profile/index.html.erb) above is the following

<%= render :partial => 'pages/page', :layout => "layouts/default", :locals => {:page => @page } %>

Now in the pages/page view there are the same default tags such as css and js files. When i remove the css styles then I lose the styling for the pages/page view. Is there a way I can render a partial without recalling the same css and js files or what is a better way to go about doing something like so?

like image 219
coletrain Avatar asked Dec 22 '22 00:12

coletrain


2 Answers

I always create the option to overwrite the stylesheets as follows:

<%= stylesheet_link_tag content_for?(:stylesheets) ? yield(:stylesheets) : "application", :debug => Rails.env.development? %>

Then inside a view

<% content_for :stylesheets %> some stuff or nothing in here <% end %>

That will let you specify in a view rendered in a layout you want no stylesheets and the same principle applies for javascripts.

That having been said if you are rendering a partial inside a layout that has an html tag and head etc. you should probably investigate if there is a better way to do what you are doing.

like image 152
Joel Jackson Avatar answered Dec 23 '22 13:12

Joel Jackson


You need to pick one or the other: layout the original method call, or pass a layout to the partials. Doing both would be illogical.

There is a more thorough discussion here:

http://www.mikemayo.org/2012/rendering-a-collection-of-partials-with-content_for

like image 28
Mark Fraser Avatar answered Dec 23 '22 15:12

Mark Fraser