3

Newer to programming, but I have accomplished some neat stuff. But given my nontraditional approach, I sometimes get frustrated and confused with the "conventional" teachings that involve a lot of assumptions.

So I have been learning rails, and like its structure, but some of the terminology is confusing me, and I have tried to understand it, but cant wrap my head around it. A lot of people keep refering me to the RailsGuides on Layouts and rendering, but I dont fully understand it, and think it lacks a broader context of the overall rails structure.

So, do you know any rails explanation sites/docs/videos/books that describe the overall processes and terminology, and more specifically, ones that address how layouts, renders, partials, and views work, relative to the rails processing?

Specifics:

The terminology gets confusing when trying to understand them in relation to one another. Take for instance the PJAX RailsCast, Bates explicitly shows that their is a difference between the layout and the template. As I am trying to accomplish this via this SO question, I am lost in the conventional structure and naming of files and their function (not everyone, just the ones that seem to be overloaded in naming).

Take for instance the applicaiton.html.erb file within views/layouts. Its file path location tells me that it is a "layout", but what about the _navigation.html.erb and _left.html.erb and the _right.html.erb etc.... that are also located in the views/layouts since all pages share these, the are in the same level as application.html.erb But these have preceding _underscores in the file name, so Rails knows these to be "partials", but they are also located in the views folder, and in the layout folder. So what name does it go by?

The issue above leads me to not understand what someone is talking about, since I also might not be structuring the files accordingly. The scoping issue (which, to my knowledge, when you call render 'action' or render 'file' etc..., rails can find another controllers view and action) plays into using render vs content_for/yield. Look at my SO question here, in Geoff's answer, in his comments when I asked, "so it is not possible to use yield from other views?", his answer was that "That is certainly not the intent".

So back to what the original question, I dont feel that these are explained as they relate to each other. I am interested in making a robust application, and I understand how it can be accomplished with other smaller frameworks, but I need to understand at a higher level how rails processes the Views (MVC, the V) using the layouts, partials, templates and views, and how the files should be structured accordingly.

I would also like either a diagram or explanation of the terms that describe them, as well as their relationship to other (usage, parent/child of another) and conventional placing. This would look something like:

A view is what is presented to the user.   It is comprised of several parts.
  The following are required [ templates, layouts ].  
  These are not required, but very likely once you have a robust and
   flexible program [ partials, yields/content_for ].   

A layout is a framework ....

A Partial is content, but only "partial" of a page....

A rendered file is ... and is located in each model's view
Community
  • 1
  • 1
chris Frisina
  • 19,086
  • 22
  • 87
  • 167
  • Hate to join the club but you should really work through the official rails guide until it clicks. In terms of books - Agile Web Development With Rails is excellent. – Damien Roche Jan 20 '13 at 22:13
  • Ive done it thrice, and have working examples Ive built outside, but some features that I think are good practices, aren't covered well (in my opinion), and I am hours away from an active physical RoR community. Thanks for the book suggestion – chris Frisina Jan 20 '13 at 22:22
  • Just thought, something else which might help you is to learn the fundamentals of MVC, particularly the VC part, not within the context of Rails, but on a general basis. – Damien Roche Jan 20 '13 at 22:54
  • Your question is pretty general... I'm not sure how I'd answer it without simply referring you to documentation you you don't understand... can you update your question to include some specific issues that confuse you? – Philip Hallstrom Jan 21 '13 at 00:34
  • @PhilipHallstrom Does that help? hope it does, If not I will try again – chris Frisina Jan 21 '13 at 01:57

1 Answers1

6

Ok... here's my view (heh heh) of things... please note that sometimes when I say "X is a Y" what I really mean is that "Keeping in line with how Rails likes to do things X is a Y".. you could violate that but most people don't... Also, none of this touches how 'render' works when called in your controllers. That's a whole other ball of wax.

  • Everything in app/views is a "view". A "view" is something that will eventually be compiled/rendered and displayed to a client (browser, 3rd party api, etc.).

  • Any file that starts with an underscore is a partial. It doesn't matter where in app/views it is... it's a partial.

  • Any [other] file is an honest to goodness view. It might be HTML, XML, JSON, PDF... but it's a view.

  • Any file in app/views/layouts that doesn't start with an underscore is also a view, but a special one. It's a layout. A layout, imho, is simply a "wrapper" of common markup that you want to apply to all of your views. You can think of them like a "template" IF you use that in the sense of "i bought some premium web templates online for my site". However, "template" has another meaning in Rails as well. So... try not to think of these layout files as templates. Think of them as, well, "layouts".

  • What makes these layout files unique is that somewhere they contain a call to <%= yield %>. Like in Ruby that yields control to something else and is replaced by the result. In Rails, this means that line will be replaced by the result of your actual view for whatever controller/action was invoked (more on this later).

  • It's totally fine to have partials in your layout directory. I do it all the time. I do it for two reasons. The first is if I want to simply my application.html.erb. A good example of this, I usually have an app/layouts/_google_analytics.html.erb file that contains the JS/logic for settings up Google Analytics. With this I can call render :partial => 'layouts/google_analytics' in application.html.erb and it's clear what's going on, but less messy. The other reason to do this is if I have more than one layout. Perhaps I have a mobile.html.erb one -- but I still need Google Analytics. Now I can include "common layout code" easily into each. It's DRY.

  • As for where other files go... put them where they make sense... where that is is usually fairly obvious and you'll get a feeling for it as you build more apps. There are some naming conventions you can adopt to use more of the Rails magic, but you don't have to. For example.. you can make calls like render @widgets and it will look for a _widget.html.erb partial and render it once for each element in @widgets. You don't have to do it this way though.

  • Shared partials? They can go anywhere. Really depends on how you like to structure things. A lot of people will create app/views/shared and put them in there. Some people will keep them with their "main" focus. That is... say you have a _recent_blog_posts partial that is used all over. You could put that in app/views/shared/_recent_blog_posts.html.erb. You could also argue it should go in app/views/blog/_recent_posts.html.erb (note the slight change to the file name). Which is right? Hard to say.

  • yield and content_for. By default Rails does this by default when it inserts your view markup into your layout. You can assume that Rails is automatically doing a content_for :default (or something close to that) when processing your view and that is how yield picks it up in your layout. But you can also do it explicitly. Say you've got a section of your website (say on the right) that you might want to tweak depending on where the user is on your site. In your layout file you could do something like this:

    ... some generic right hand side stuff...

Then, in your view file you can do this:

... my main view content...
<% content_for :right_side do %>
  ... specific right side content stuff...
<% end %>

And when it all comes together Rails will put that specific right side content into the the right side of the layout.

Anyway... hope that helps.

Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
  • can you explain what templates are? you said `However, "template" has another meaning in Rails as well.` but dont follow up what they are – chris Frisina Jan 22 '13 at 02:45
  • @chrisFrisina I guess I mostly meant that one can think of layout files as "templates". There is also an optional argument to the render method ":template". Not a big deal, but it does muddy the waters imho. – Philip Hallstrom Jan 22 '13 at 18:00