Rails handle partial without full html layout

Ok, so I have a problem with rails and partial rendering. I have a layout called profile, and inside the profile layout I have all my js, style sheets, etc.

<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 the yield tag (profile / index.html.erb) above is indicated

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

Now the page / page displays the same default tags as the css and js files. When I delete the css styles, I lose the style for viewing pages / pages. Is there a way I can make it partial without recalling the same css and js files, or is that the best way to do something like this?

+4
source share
4 answers

You need to choose one or the other: mock the original method call or pass the mock to parts. To do both would be counterintuitive.

There is a more thorough discussion here:

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

+3
source

I always create the ability to overwrite style sheets as follows:

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

Then inside the view

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

This will allow you to indicate in the view presented in the layout that you do not want the style sheets to apply the same principle for javascripts.

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

+4
source

I rarely see use (or I wonder if Rails supports this use ...)

 <!-- confirmed, this usage will cause error in Rails 3.2 --> <%= render :partial => "some_partial", :layout => "some_layout" ... %> 

I prefer to choose a specific layout in the controller:

 def some_action # some code render :layout => "some_layout" end 
+3
source

In part, this is simply a “page fragment” (for example, a piece of cake ... but in the form of code). It is intended to fill a small part of the page; usually one that will be dynamically updated depending on the page variables.


It seems that you are confusing the purpose of layouts, views and partitions, in my opinion. If you want to dynamically load CSS / JS, put the " content_for " block in your profile views using the default:

Markup

 #layouts/default.rb <html> <head> <title>Site Title</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" %> <%= yield :header_includes %> <%= csrf_meta_tag %> </head> <body> <%= yield %> </body> </html> 

Views

 #views/profiles/index.html.erb <% content_for :header_includes do %> <%= stylesheet "profile_custom_css" %> <% end %> 

Partial

Particles can be used to store DRY code and output the output of certain header files, for example:

Partial

 #views/elements/_custom_header.rb <% content_for :header_includes do %> <% headers.each do |type, value| %> <% if type == "java" %> <%= javascript_include_tag value %> <% else %> <%= stylesheet_link_tag value %> <% end %> <% end %> <% end %> 

View

 #views/profiles/index.html.erb <%= render :partial => 'elements/custom_header', locals: { :headers => [["java", "profile_custom"], ["stylsheeet", "profile_custom"]] } %> #Resume standard view code here 

Markup

 #layouts/default.rb <html> <head> <title>Site Title</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" %> <%= yield :header_includes %> <%= csrf_meta_tag %> </head> <body> <%= yield %> </body> </html> 

I have not tested passing partial locales as a hash, so the syntax may not be right, but this is what we would do to load the required code. An added benefit is that content_for only gives the content that was defined (IE you just have to include yield :custom_headers and it will only show if a content block is present)

+1
source

Source: https://habr.com/ru/post/1403276/


All Articles