27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

First steps<br />

Then enter the following code in app/views/layouts/application.html.erb where the<br />

title tag currently is.<br />

Listing 3.23 app/views/layouts/application.html.erb<br />

<br />

In Ruby, instance variables that aren’t set return nil as their values. If you try to access<br />

an instance variable that returns a nil value, you can use || to return a different<br />

value, as in this example.<br />

With this in place, your step passes when you run rake cucumber:ok:<br />

And I should see "TextMate 2 - Projects - Ticketee"<br />

With this scenario now passing, you can change your code and have a solid base to<br />

ensure that whatever you change works as you expect. To demonstrate this point,<br />

change the code in show to use a helper instead of setting a variable.<br />

Helpers are methods you can define in the files inside app/helpers, and they are<br />

made available in your views. Helpers are for extracting the logic from the views, as<br />

views should just be about displaying information. Every controller that comes from<br />

the controller generator has a corresponding helper, and another helper exists for<br />

the entire application. Now open app/helpers/application_helper.rb and insert the<br />

code from the following listing.<br />

Listing 3.24 app/helpers/application_helper.rb<br />

module ApplicationHelper<br />

def title(*parts)<br />

unless parts.empty?<br />

content_for :title do<br />

(parts

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!