Fleeting Thoughts

For anything greater than 140 characters...

Posts tagged rails,

Jun 28

Rails Part 2: Adding a Catalog Display

So - on to Part 2 - and I’ve covered the following ground…

  • Created a new controller to handle customer interactions

Using the ruby script/generate command again, but this time adding a controller rather than a scaffold.

  • Implemented the default index action.

In the script/generate command, to implement the index action, simple add the word ‘index’ after the controller name (‘store’)

  • Added a class method to the Product model to provide a list of items for sale.

Back over in Products.rb, a small method was added (using self.methodname to ensure that it is a class method rather than an instance method) to list out the products.

  • Implemented a view (via a .html.erb file) and a layout that contains it

The index.html.erb view was the main way to create specific ‘index’ content, but by creating a store.html.erb file in the app/views/layout folder I managed to create an overall layout for the component that uses the expression <% yield: layout %> to include other pages within this layout. 

  • Use a helper to format prices the way I want

Nice and easy to implement via <%= number_to_currency() %> - seems to default to US Dollars, so I’m not sure how that would work if you want to work with e.g. GBP….but I guess we’ll see?

  • Added a button to each item to allow people to add it to the cart.

A simple modification to the index.html.erb template with a rails helper <%= button_to ‘Add to Cart’ %> making things easy.

  • Made a simple modification to a stylesheet.

Using Dreamweaver (to make CSS modifications easy), updates to the provided depot.css allowed me to make a nice clean template, and to orientate the buttons within the main page. CSS - so powerful, but incredibly dull to learn, eh?

Also completed the ‘extra’ exercises - adding time/date to the side bar was straightforward using <%= Time.now %> in the store.html.erb. 

Adding a hyperlink to the book’s image was a little bit more tricky and required a deeper look at the link_to syntax:

<%= link_to image_tag(product.image_url),

      { :action  => :add_to_cart, :id => product },

      :post  => true %>

Finally, tinkering around with the number_to_currency() helper method allowed me to add GBP and other currency types to the formatting. Options are available to specify :unit, :separator, and :delimiter .

So - that’s the second part of the tutorial completed. In the next lesson, I’m learning how to create the shopping cart itself! See you then!


Jun 27

Learning Rails. Again.

I’ve decided to remind myself of how good Rails is as a development and prototyping framework after my 6-month dalliance with the Semantic Web.

Picking up my dusty PragProg tome (Agile Web Development with Rails, Third Edition) I got down to business with hacking together their example of a web site cart, which will hopefully refresh my knowledge of all the Rails essentials whilst building something vaguely useful.

Task A was building a product maintenance screen - took me about an hour. 

The steps included:

  • Creating a development database (SQLLite3) and configuring the Rails application to access it.

This step was almost trivial - I’ve forgotten how easy a Rails scaffold is to implement, although I’m still thinking that its main purpose is to convince newbie Rails developers that ‘Hey, Rails is easy’! 

  • Used migrations to create and modify the schema in my development database and to load test data.

Already, I can sense that migrations are a really cool way of tracking change in a development - the Rails framework ‘knows’ how to apply changes based on the UTC timestamp on the migration file. Using a migration script to load in test data is really handy, as all entered data gets wiped upon each reload.

  • Created a products table and used the scaffold generator to write an application to maintain it.

With Rails, it’s a one-line script to generate the scaffold, which does 90% of the work. However, then you spend the next 30 mins editing the scripts…is it faster to create from scratch…?

  • Augmented the generated code with validation.

Since Rails is MVC (Model-View-Controller)-based, all of the validation so far is in the model (products.rb) - again, adding checks for presence, ‘numericality’ (love that) and custom logic is really nice and straightforward…custom functions dip into hand-coded Ruby so it’s nice to get a refresher on that too.

  • Rewrote the generic view code with something prettier.

Well…copied some files from the Pragprog.com website…not sure *I* did much here, but it sure does look purrrrty! :-)

Will update more when I move onto the next task - Catalog display.