OverviewFlexstore is a traditional Shopping Cart application. In this tutorial, we create two modules:
- The administration module is an
internal application used to maintain the product database. You use the administration module to create, update, and delete products.
- The store module is a
customer-facing application. Customers use the store module to browse and filter the product catalog.
In the administration module, we use Rails' simple scaffolding feature to automatically provide the default infrastructure to list, view, create, edit, and delete products. In the store module, we experiment with additional features:
- Templates
- Filtering using Ajax
- Partial Page Templates
- Builder Templates
- Putting a Flex front-end on top of a
Ruby on Rails application
This tutorial uses a MySQL database. It is assumed that you already have MySQL up and running.
Create the Flexstore Database
- Create a database called "flexstore"
Open a command prompt, navigate to the bin directory of your MySQL Server installation, and type the following command:
mysqladmin -uroot create flexstore
Note: Provide values for userid (-u) and password (-p) as appropriate if the above values don't match your installation.
- Import the data
Install Ruby and Rails
- Install Ruby
- Download the Ruby installer here
- Run the installer. Accept all
the default settings.
- Install Rails
Type the following command in the c:\ruby directory:
gem install rails --remote -- include-dependencies
Create the Administration module
- Create the flexstore application
- Create a directory called rails in c:\
- Type the following command in c:\rails:
rails flexstore
- Configure the database for the flexstore application
- Edit database.yml in c:\rails\flexstore\config
- Set the database parameter to
flexstore in the development, test, and production sections
- Create a controller for the administration
module
Type the following command in c:\rails\flexstore
ruby script\generate controller Admin
- Create a model for the products
Type the following command in c:\rails\flexstore:
ruby script\generate model Product
- Modify the admin controller to enable Rails' scaffolding
- Edit admin_controller.rb in c:\rails\flexstore\app\controllers
- Modify the class as follows:
- Test the application
- Start the WEBrick web server installed with Rails
Type the following command in c:\rails\flexstore:
ruby script\server
- Open a browser and access the following URL:
http://localhost:3000/admin
Rails' scaffolding defined in the Admin controller automatically provides default actions and views to list, view, create, edit and delete products. Each of these actions and views can be overwritten. We overwrite the default index action in the next steps.
- Define a custom index action
- Edit admin_controller.rb in c:\rails\flexstore\app\controllers
- Define an index action as follows:
- Create the view for the index action
- Create a file name index.rhtml in c:\rails\flexstore\app\views\admin
- Edit index.rhtml as follows:
<html> <head> <title>Product List</title> </head> <body> <table> <tr> <td>Name</td> <td>Price</td> </tr> <% @products.each do |product| %> <tr> <td><%= link_to product.name, :action => "show", :id => product.id %></td> <td align="right"><%= sprintf("$%0.2f", product.price) %></td> </tr> <% end %> </table> <p><%= link_to "Create new product", :action => "new" %></p> </body> </html>
- Test the application. Open a browser and access the following URL:
http://localhost:3000/admin
- Validation
- Edit product.rb in c:\rails\flexstore\app\models
- Modify the class as follows:
- Test the application again. Try to add a product without a name or with a non numeric price value.
Create the Store module
- Deploy the product images and the flexstore stylesheet
- Download assets.zip here.
- Extract assets.zip in c:\rails\flexstore\public.
- Create a controller for the administration module
Type the following command in c:\rails\flexstore:
ruby script\generate controller Store
- Define an index action in the Store controller
- Edit store_controller.rb in c:\rails\flexstore\app\controllers
- Define an index action as follows:
- Create the view for the index action
- Create a file name index.rhtml in c:\rails\flexstore\app\views\store
- Edit index.rhtml as follows:
<html> <head> <title>Flexstore on Rails</title> <%= stylesheet_link_tag "flexstore", :media => "all" %> </head> <body> <!-- begin catalog --> <div id="catalog"> <% for product in @products %> <!-- begin thumbnail --> <div class="thumbnail"> <strong><%= product.name %></strong> <img src="<%= product.image %>"/> <div> <font color="#CC6600"><b><%= sprintf("$%0.2f", product.price) %></b></font> <p> <%= product.camera==1?'Camera<br />':'' %> <%= product.video==1?'Video<br />':'' %> <%= product.triband==1?'Triband':'' %> </p> </div> </div> <!-- end thumbnail --> <% end %> </div> <!-- end catalog --> </body> </html>
- Test the application
Open a browser and access the following URL:
http://localhost:3000/store
Using Partial Page Templates
In real life, you would probably have to display product thumbnails in different parts of the application. In fact, we will need product thumbnails in the filter module described in the next section. To avoid code duplication, we will isolate the HTML fragment used to render a product thumbnail in a partial page template.
- Create a file named _product.rhtml (the partial page template) in c:\rails\flexstore\app\views\store
- Open index.rhtml in c:\rails\flexstore\app\views\store
- Copy the HTML fragment corresponding to the thumbnail div and paste it in _product.rhtml
- In index.rhtml:
- Delete the content of the catalog
div (both the for loop and the thumbnail div)
- Add the following line of code as
the sole content of the catalog div:
<%= render(:partial => "product", :collection => @products) %>
Because we are passing a collection (the list of products) as a parameter, the partial page template will be repeaded for each item in the collection.
- Test the application. The product catalog should look the same as in the previous section.
http://localhost:3000/store
Filtering with Ajax
In this section, we provide the product catalog with filtering capabilities to allow the user to specify a price range. In response to the user's selection, the product catalog is refreshed to display the phones in the selected price range only. For a better user experience, the product catalog is refreshed without refreshing the entire page. This is accomplished using Rails' built-in support for Ajax.
- Add a filter action to the store controller
- Modify the index view to allow the user
to enter a price range
- Change the id of the catalog div to "right". This will allow the stylesheet to position the product catalog to the right of the filter panel.
- Test the application
http://localhost:3000/store
Builder Templates
Using Builder templates, you can dynamically generate XML documents. This provides an integration point that allows other technologies to integrate with Rails, and leverage productivity features of the framwework. In this section, we create a template that generates an XML document for the product catalog.
- Add a productlist action to the store controller
- Edit store_controller.rb in c:\rails\flexstore\app\controllers
- Define a productlist action as follows:
- Create the builder template
- Test the Builder template by accessing the following URL in a browser:
http://localhost:3000/store/productlist
You should see an XML document similar to the one below:
Putting a Flex Front-End on Top of the Rails Application
In this section, we use Flex to improve the user experience of the product catalog. The user interface to capture the filtering criteria is still implemented in HTML.
The Flex-based product list uses the Builder template created in the previous section to retrieve the catalog data. Contrary to our current version, the Flex-based list handles product filtering at the client-side. Combined with the use of rich effects and transparency, this provides the user with smoother transitions between selections. These transitions implement the User Interface Design best practise of "visual continuity", and include visual cues indicating which products are being filtered out and filtered in.
- Define a flex action
- Edit store_controller.rb in c:\rails\flexstore\app\controllers
- Define a flex action as follows:
- Deploy catalog.swf (the compiled version
of the Flex-based product catalog).
- Download flexcatalog.zip here.
- Extract flexcatalog.zip in c:\rails\flexstore\public
Notes:
- This application was built using the Flex XML-based framework. You can download the source code here.
- The application was built with Flex 2 beta and requires Flash Player 8.5 beta 2 available here.
- First Cut
In our first iteration, the user interface of the application remains very similiar to the HTML version. We simply replace the HTML-based product list with a Flex-based version.
- Optimizing the user experience
In this second iteration, we optimize the filtering experience: We use sliders (from the Yahoo UI library) to select the price range. The Flex-based product list reacts to the user's selection as the sliders are dragged.
Resources
|
3 comments:
Does anyone have the files this tutorial links to? His site is still down.
hello
try this link instead:
http://webddj.sys-con.com/read/223793.htm
good luck
:)
After I download a copy of Flex 2 SDK, it comes with sample of the product dB , with assets like CSS and image files.
The download files are removed, but you can retrieve same dataset from Flex 2 SDK samples - flexstore
good luck
:)
Post a Comment