27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

268 CHAPTER 10 Tracking state<br />

You have state transition showing in your application neatly, which is great to see. Let’s<br />

commit and push this to GitHub:<br />

git add .<br />

git commit -m "Display a comment's state transition"<br />

git push<br />

Currently, your styles aren’t distinguishable. Look at figure 10.14, and gaze upon their<br />

ugliness.<br />

You could distinguish them by using the colors<br />

you’ve specified in the attributes. Earlier, you<br />

wrapped the state name in a special div that will<br />

allow you to style these elements, based on the<br />

class. For the New state, the HTML for the div Figure 10.14 The unescaped states<br />

looks like this:<br />

<br />

New<br />

<br />

The state_new part of this you can use to apply the colors from the record to this element.<br />

To do this, you put a style tag at the top of your application’s layout and<br />

dynamically define some CSS that will apply the colors.<br />

10.3.6 Styling states<br />

The states in your system can change at any point in time in the future, and so you<br />

can’t have set styles in public/stylesheets/application.css for them. To get around this<br />

little problem, put a style tag in your app/views/layouts/application.html.erb file,<br />

which will contain some ERB code to output the styles for the states. Directly underneath<br />

the stylesheet_link_tag line, put this code:<br />

<br />

<br />

.state_ {<br />

background: ;<br />

color: ;<br />

}<br />

<br />

<br />

You need to define the @states variable in a place that will be accessible in all views of<br />

your application. This means you can’t define it inside any controller other than<br />

ApplicationController. Lucky for you, this is like a normal controller, and you can use<br />

a before_filter to load the states. Underneath the class definition for Application-<br />

Controller, you can add this before_filter:<br />

before_filter :find_states<br />

Now you define the method under the authorize_admin! definition:

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

Saved successfully!

Ooh no, something went wrong!