Lost Password?
Register now!
Page « 1 ... 4 5 6 (7) 8 9 »
Articles : Web Development Using the Ruby on Rails
on 2008/7/27 23:54:31 (2893 reads)

We have a ContractsController with an action 'destroy'. Obviously this action deletes a certain contract. Lets presume there is a form on a Web page that lets the user delete a contract by pressing a DELETE button. The action 'destroy' starts with looking up the contract to delete using the Contract model: Contract.find_by_number(params[:number]).

The Contract model seems to have a method with the name of the column of interest, 'number' in it. How is it possible? Well, just like Rails adds attributes to the model class based upon the names of the columns of the table it belongs to, it also adds 'find_by_' methods for each column name. As with the automatically added attributes, it doesn't matter if you decide to rename a database column later on, since all this code is added by Rails at runtime. You never have to maintain this generated code! See how Rails gives you a powerful framework to work and yet doesn't require you to write much code?

Back to our example, after the contract is retrieved, it is removed from the database by calling the method 'destroy', nothing earth-shocking there. The next line is something we have not seen before: redirect_to 'show'. In the previous example, we assumed we wanted to render the loaded information, but in this case we didn't load any information, quite the opposite even! We want to show the user an overview of all remaining contracts after the user deletes one. We will add a separate action 'show' for that. This method can be used by visiting the url 'http://www.myrailsapp.com/contracts/show'. The 'redirect_to' statement lets Rails execute the 'show' action immediately after executing the 'destroy' action. The 'show' action doesn't state anything explicitly after loading all the contracts so it assumes it needs to render a Web page (located in /views/contracts/show just like in the employee example). We could explicitly say to render a Web page if we wanted to by saying: render 'show'.

The View

Our ride through Rails is almost done, only the View layer is left. It can be found in the ActionView bundle and is the easiest one of the three. The View is comprised of all the rhtml pages found in the 'views' directory, as shown in the Controller examples. There is not much difference between a plain html file and a rhtml file, except that a rhtml file may contain small pieces of embedded Ruby code, called ERb.

Rails defines some helper methods for use on rhtml pages that make it easy to generate links, forms etc in your pages. There is also support for AJAX, that nifty JavaScript technology that lets you make Web pages that behave almost like a rich client. You can create typical AJAX effects, like submitting a form without refreshing the entire page, without even having to write any JavaScript yourself.

There is one other important thing to know about rhtml pages in Rails: it is possible to use a template system, called Layout. It would be really cumbersome if we would have to write each rhtml page beginning with the opening tag until the closing tag, especially given the fact that most pages will probably have a lot of html in common, for example a navigation bar. The template system takes care of this:

<%= yield %>

Page « 1 ... 4 5 6 (7) 8 9 »
Printer Friendly Page Send this Story to a Friend Create a PDF from the article