Tailoring the Generated Scaffold Code
|
|
|
|
| Articles Reviews Ruby | |
| Written by Bogdan V | |
| Tuesday, 12 December 2006 | |
|
{mos_sb_discuss:50}
The Controller In a ‘List’ view, I would expect the records to be displayed in alphabetical order. This requires a minor change to the controller: app\controllers\categories_controller.rb (excerpt) def list @category_pages, @categories = paginate :category, :per_page => 10, :order_by => 'category' end In this application, the show screen is unnecessary – all the fields fit comfortably on a single row on the screen. So, def show can disappear, and let’s go straight back to the list screen after an ‘Edit’: app\controllers\categories_controller.rb (excerpt) def update @category = Category.find(@params[:id]) if @category.update_attributes(@params[:category]) flash['notice'] = 'Category was successfully updated.' redirect_to :action => 'list' else render_action 'edit' end end The flash message will be picked up and displayed on the next screen to be displayed – in this case, the list screen. By default, the scaffold script doesn’t display flash messages - we’ll change this in a minute – see below. The View Displaying Flash Messages Rails provides a technique for passing ‘flash’ messages back to the user – e.g. an ‘Update Successful’ message which displays on the next screen and then disappears. These can be picked up easily with a small change to the Layout (adding it to the Layout means it will appear on any screen): app\views\layouts\categories.rhtml<html> <head> <title>Categories: <%= controller.action_name %></title> Powered by jReviews |
|
| Last Updated ( Sunday, 08 July 2007 ) | |
| < Prev | Next > |
|---|







