Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In your "app/views" directory there is a folder called "layouts." By default there should be an "application.html.erb" file in there, but if there isn't you can create it.</p> <p>Your "application" layout file is the default layout file used by any view. However, if you want a particular controller to use a different view, you can override this. See this <a href="http://railscasts.com/episodes/7-all-about-layouts" rel="nofollow">railscast</a>, and <a href="http://railscasts.com/episodes/8-layouts-and-content-for" rel="nofollow">this one</a> is helpful too.</p> <p>The main thing to understand is the content from any particular view will show up wherever the <code>yield</code> method appears in your application layout. The main 'yield' block gets the view file specified by your controller action, but you can mark anything inside any view to be passed to another yield block instead. For instance, the "title" example you gave could be passed to the head of your application layout. See this <a href="http://railscasts.com/episodes/30-pretty-page-title" rel="nofollow">railscast</a> for a detailed example of that.</p> <p>For more, you should read the <a href="http://guides.rubyonrails.org/layouts_and_rendering.html" rel="nofollow">Rails Guide</a>, and you might want to consider picking up a Rails starter book.</p> <p>I got my feet wet with "<a href="http://rads.stackoverflow.com/amzn/click/1430224339" rel="nofollow">Beginning Rails 3</a>," which was a phenomenal introduction to the framework. A couple days with that book and it was all making sense to me, and I was developing faster than I ever had before. Rails rocks once you get to know it, but it's definitely worth going through a book.</p> <p>Please continue to ask questions, I'll help if I can :)</p> <p>-EDIT- To answer your question about control flow, it basically works like this:</p> <ol> <li><p>Your browser sends a GET request for a particular URL.</p></li> <li><p>The router takes that request, matches it to a controller action, triggers that controller action, and provides the controller any parameters associated with the request. For instance: if you requested <code>example.com/posts/123?color=red</code> this would trigger the <code>SHOW</code> action of your <code>posts_controller</code>, and would pass {:color => 'red'} to the params hash. You would access that using <code>params[:color]</code></p></li> <li><p>The controller action does its thing, and when it's done it renders output. By default it renders whatever view is located in <code>app/&lt;controller_name&gt;/&lt;action_name&gt;</code>, and will whichever file matches the extension appropriate to the request (ie an AJAX request would trigger <code>&lt;action_name&gt;.js.erb</code> and a GET request would trigger <code>&lt;action_name&gt;.html.erb</code>.</p> <ul> <li><p>You can override this using the <code>render</code> method, for example by passing <code>render 'foo/bar'</code> to render using the view for FooController, Bar action instead of your current action.</p></li> <li><p>Note that no matter what you render, the data available to the view is whatever is in the specific controller action the router triggered, not the controller action that would 'normally' render that view.</p></li> </ul></li> <li><p>The view file is parsed using the data from the controller that called it. If you have any <code>content_for</code> methods then the view code that is inside the <code>content_for</code> block will go where you tell it, otherwise everything else will go to the main YIELD block in your <code>application</code> layout (or whatever layout your controller specified instead).</p></li> <li><p>The application layout is parsed, and the content from the view is inserted into the appropriate areas.</p></li> <li><p>The page is served to the user.</p></li> </ol> <p>That's a simplification in some ways, but I think it answers your question. Again, feel free to keep asking :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload