Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: What does it actually mean to "render a template"

I've become a bit confused about the idea of "rendering" a "template" due to the way an author speaks about it in a book I'm reading.

My original understanding of "rendering a template" was that it meant that Rails is providing the content that is viewed on the screen/presented to the viewer (in the way that a partial is rendered) but the book I'm reading seems to be using the concept of "rendering a template" to also mean something else. Let me explain in context

This book (rails 3 in action) sets up a page layout using the conventional layouts/application.html.erb file, and then it "yields" to different view pages, such as views/tickets/show.html.erb which adds more content to the screen. that's all straightforward..

Within this view views/tickets/show.html.erb, there is a rendering of a partial (which is also a straightforward concept).

<div id='tags'><%= render @ticket.tags %></div>

Now within this partial there is, using ajax, a call to a "remove" method in the "tags_controller.rb" which is designed to allow authorized users to remove a "tag" from a "ticket" in our mock project management application.

<% if can?(:tag, @ticket.project) || current_user.admin? %>
    <%= link_to "x", remove_ticket_tag_path(@ticket, tag),
      :remote => true,
      :method => :delete,
      :html => { :id => "delete-#{tag.name.parameterize}" } %>
  <% end %>

Now here is the "remove" action in the tags controller (which disassociates the tag from the ticket in the database)...

def remove
    @ticket = Ticket.find(params[:ticket_id])
    if can?(:tag, @ticket.project) || current_user.admin?
      @tag = Tag.find(params[:id])
      @ticket.tags -= [@tag]
      @ticket.save
    end
  end
end

At the end of this remove action, the author originally included render :nothing => true , but then he revised the action because, as he says, "you’re going to get it to render a template." Here's where I get confused

The template that he gets this action to render is "remove.js.erb", which only has one line of jquery inside it, whose purpose is to remove the "tag" from the page (i.e. the tag that the user sees on the screen) now that it has been disassociated from the ticket in the database.

$('#tag-<%= @tag.name.parameterize %>').remove();

When I read "rendering a template" I expect the application to be inserting content into the page, but the template rendered by the "remove" action in the controller only calls a jquery function that removes one element from the page.

If a "template" is "rendered", I'm expecting another template to be removed (in order to make room for the new template), or I'm expecting content to be "rendered" in the way that a partial is rendered. Can you clarify what is actually happening when a "template" is "rendered" in the situation with the jquery in this question? Is it actually putting a new page in front of the user (I expected some sort of physical page to be rendered)

like image 771
Leahcim Avatar asked Dec 23 '11 06:12

Leahcim


2 Answers

You're nearly there! Rendering a template is indeed always about producing content, but for a slightly wider description of content. It could be a chunk of html, for example an ajax call to get new items might produce some html describing the new items, but it doesn't have to be.

A template might produce javascript as it does in your second example. Personally I am trying to avoid this and instead pass JSON back to the client and let the client side js perform the required work.

Another type of rendering you might perform is to produce some JSON. APIs will often do this, but you might also do this on a normal page. For example rather than rendering some javascript to delete tag x you might render the json

{ to_delete: "tag-123"}

and then have your jQuery success callback use that payload to know which element to remove from the DOM, by having this in your application.js file

$('a.delete_tag').live('ajax:success', function(data){
  var selector = '#' + data.to_delete;
  $(selector).remove()
}

(Assuming that your delete links had the class 'delete_tag') Rendering JSON like this isn't really a template at all, since you'd usually do this via

render :json => {:to_delete => "tag-#{@tag.name.parameterize}"}

although I suppose you could use an erb template for this (I can't imagine why though).

like image 114
Frederick Cheung Avatar answered Sep 30 '22 22:09

Frederick Cheung


My understanding is that js.erb is "rendered" by executing the javascript functions within it. Very often something like the below is done:

jQuery(document).ready(function() {
    jQuery('#element').html('<%= escape_javascript(render pages/content) %>');
});
like image 36
prusswan Avatar answered Oct 01 '22 00:10

prusswan