Tuesday, April 22, 2014

Layouts and Rendering in Rails

K is having me read: http://guides.rubyonrails.org/layouts_and_rendering.html
To understand what this is doing: <%= render partial: "file/metadata" %>

I already know that it is embeded ruby and with the inclusion of the = sign it prints whatever is in there to the view but what am I missing that is wrong?

The render method
render - read the files interprets the filepath

ActionController::Base#render https://gist.github.com/jcasimir/1210155

The render method is very overloaded in Rails. Most developers encounter it within the view template, using render :partial => 'form' or render @post.comments, but here we'll focus on usage within the controller. http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html


partial  - doesn't wrap (on either side) the contents in the layout  file
Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.

A partial can use its own layout file, just as a view can use a layout.

http://guides.rubyonrails.org/layouts_and_rendering.html

Controller and View in the Model-View-Controller The Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.

Controllers in Rails automatically render views with names that correspond to valid routes.

The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the action_name.html.erb template in the controller's view path and render it.

Structuring Layouts
When Rails renders a view as a response, it does so by combining the view with the current layout, using the rules for finding the current layout that were covered earlier in this guide. Within a layout, you have access to three tools for combining different bits of output to form the overall response:

  • Asset tags
    • auto_discovery_link_tag
    • javascript_include_tag
    • stylesheet_link_tag
    • image_tag
    • video_tag
    • audio_tag
  • yield and content_for
  • Partials

My Ah ha moment:

I had copied and pasted the exact same information into the file as was being rendered by <%= render partial: "file/metadata" %> So it was there twice in different forms.


No comments:

Post a Comment