Two-Stage Rail Layouts

My web pages consist of two parts, albeit at the top and bottom (except for the header and footer - they are consistent between the pages). What is the best practice for dynamically creating these parts based on the action?

One of the approaches that I came up with is to have an idea of ​​the upper and partial for the bottom; in the output call the layout for the top and partial for the bottom. The partial file name is dynamically replaced depending on the action.

Not sure if this is the best way to do this.

+5
source share
2 answers

I think your idea is fine. In your views you can:

<%- content_for :top do -%>
  […]
<%- end -%>

<%- content_for :bottom do -%>
  <%= render @partial_name %>
<%- end -%>

, , . , .

:

<div id="top">
  <%= yield :top %>
</div>

<div id="bottom">
  <%= yield :bottom %>
</div>
+8

DSL, . . , " " ( , ..).

# app/views/shared/_screen.erb
<div id="screen">
  <div class="screen_header">
 <%= yield :screen_header %>
  </div>
  <div class="screen_body">
 <%= yield :screen_body
  </div>
  <div class="bottom">
    <%= yield :footer %>
  </div>
</div>

# app/helpers/screen_helper.rb
module ScreenHelper

 def screen(&block)
  yield block
  concat(render :partial => 'shared/screen')
 end

 def screen_header
   content_for :screen_header do
   yield
  end
 end

 def screen_body
  content_for :screen_body do
   yield
  end
 end

 def footer
  content_for :footer do
   yield
  end
 end
end

# app/views/layouts/application.erb
# only showing the body tag
<body>
  <%= yield :layout
<body>

# Example of a page
# any of the sections below (except screen) may be used or omitted as needed.
# app/views/users/index.html.erb
<% screen do %>
  <% screen_header do %>
  Add all html and/or partial renders for the header here.
  <%end%>
  <% screen_body do %>
    Add all html and/or partial renders for the main content here.
  <% end %>
  <% footer do %>
 Add all the html and/or partial renders for the footer content here.
  <% end %>
<% end %>
+1

All Articles