Rails: about the crop

I saw some code in a Rails v2.3 application.

In layout/car_general.html.erb (this view is called by a method in cars_controller), I saw the code:

 <body> <%= yield %> <%= javascript_include_tag 'jquery-1.4.2.min' %> <% javascript_tag do %> <%= yield :jstemplates %> var some_car = new Object; <%= yield :some_car %> <% end -%> </body> 

Two questions:

  • Where can I find the yield content of the first% <% = yield%> under <body> .
  • Is this a special way to use js code in a view using <%= yield :jstemplates %> and what about <%= yield :some_car %> , does it point to the view or just show some_car value?
+28
ruby-on-rails ruby-on-rails-3
Oct 21 '11 at 10:18
source share
1 answer

Without any arguments, revenue will display the current controller / action template. Therefore, if you are on the cars/show page, it will display views/cars/show.html.erb .

When you pass the yield argument, it allows you to define content in your templates that you want to display outside of that template. For example, if your cars/show page contains a specific html fragment that you want to render in the footer, you can add the following to your show template and the car_general layout:

show.html.erb:

 <% content_for :footer do %> This content will show up in the footer section <% end %> 

layouts / car _general.html.erb

 <%= yield :footer %> 

The Rails Guide has a good section on using yield and content_for: http://guides.rubyonrails.org/layouts_and_rendering.html#understanding-yield

The API documentation for content_for also useful and has some other examples. Note that this is for Rails 3.1.1, but this functionality has not changed much since 2.3, if at all, and should still apply for 3.0.x and 3.1.x.

+49
Oct. 21 '11 at 10:28
source share



All Articles