Developers may continue to build upon work with the OTBS using the topic reading to help with user registration and advanced login features from Hartl et al (2008).
- generate a controller and an action by adding a method(s) to a controller;
- create a view template for each action and to link to actions from views;
- use AJAX to improve the user experience;
I have started work on this workshop by reading and following up to chapter 8 of the online book RailsSpace: Building a Social Networking Website with Ruby on Rails. I have made some notes along the way at RailsSpace: Building a Social Networking Website with Ruby on Rails - notes.
I will now add the user registration and advanced login features to my Online Taxi Booking System (OTBS). Advanced login features include logging in and out and remembering if a user is logged on.
I started with the OTBS system in Workshop 3.
The first thing I need to do is include a .css file so that it looks a bit better than it did before. I have used the css file from the Rails_Space project but changed the colour to dark blue. (If this was a real project I would have got a design person to do the .css file as I am like most programmers and don't have a design bone in my body!!)
Next, I need to convert the No of passengers into a drop down list and the Type of Taxi text field into radio buttons in the new.html.erb and the edit.html.erb forms. Because we are using Form Helpers the code is as follows:
<%= f.radio_button :taxi_type, 'Sedan' %> Sedan
<%= f.select :passenger_number, (["1", "2", "3", "4", "5", "6"]) %>
This may look simple but it has taken me at least an hour to work out how to do it. There are lots of answers on the web but they all seem to be for the previous version or something. Whatever, they don't work. Also there is probably a better way to get the sequence of numbers, but I haven't got a lot of time to look into it at the moment.
Decided to drop the job_id field as I already have an autoincrement id in the passengers table.
ALTER TABLE otbs.passengers DROP COLUMN job_id; (I ran this in mysql and it will probably come back to bite me.)
Changed the time field to only show the time using f.time_select eg
<%= f.time_select :time_required%>
For date handling the following article is very helpful:
Date Handling in Rails applications
In the tradition of DRY I created a partial with the following code.
<% form_for(@passenger) do |f| %>
<%= render :partial => "display_booking", :locals => { :f => f, :type => "Create" } %>
<% end %>
This is necessary because the rendering of the New Booking and Edit Booking used the same html code. I just needed to pass a parameter for the form and also for the text to show on the submit button. "Create" for New Booking and "Update" for Edit Booking
Created a user controller for the login/logout with the following code:
ruby script/generate controller user
also one for the site:
ruby script/generate controller site
To validate the input on the form I have used the following:
validates_presence_of :name, :contact_number, :street, :suburb_origin, :street_number, :suburb_destination, :passenger_number, :date, :time_required
validates_length_of :name, :street, :suburb_origin, :suburb_destination , :within => 3..40
validates_length_of :contact_number, :minimum => 6
validates_numericality_of :contact_number
validates_uniqueness_of :name, :scope => [:date, :time_required], :message => "You already have a taxi booked for this date and time"
See below for an example of the error messages:

To get the advanced login working I leveraged (stolen if you prefer) the files from rails_space\app\views\users and copied to my otbs directory.
I also copied the migration files from rails_space\db\migrate to otbs and ran the following command to create the users table:
rake db:migrate
I have also been thinking about the focus question "As either a developer or as an IT manager, how can aspects of social networking be applied to the OTBS?" and I think that what is needed in the application is the following:
A blog
I decided to create a blog. I found a good article on how to do this called How to create a blog with Rails 2 so followed the instructions.
Ran the following scripts:
ruby script/generate scaffold post title:string body:text username:string created_at:datetime
rake db:migrate
I have learnt a valuable lesson: don't drop tables in mysql. Instead use the rake command to go to a particular verion. This command uses the self.down method to remove what was added in the version you want to get rid of. The command is:
rake db:migrate version=XX.
If you drop a table without doing the above when you try and revert back you keep getting rake errors because in the self.down it says to drop the table but it doesn't exist any more because you already deleted it. Comment the drop_table command in the self.down and then it will work eg:
def self.down
# drop_table :posts
end
When formatting dates and times you uses the strftime function eg:
<%= post.created_at.strftime('%d %b %Y %H:%M') %>
which displays:
23 July 2009 20:18
to redirect to a different controller:
redirect_to :controller => "user", :action => "login"
After creating a new controller don't forget to restart the web server (should have followed my own advice)
To create the database tables I needed I just copied the migrations and then did a db:migrate command. If you do this you will get the following error:
undefined local variable or method `new_page_path' for #
This is because the config\routes.rb has to have the following:
map.resources :pages
A wiki
I also decided to create a wiki so I used the Learning Rails examples as a base. I used the gem called RedCloth and the plugins called acts_as_textiled and in_place_editing as per the podcast Lesson 14. I had to change things slightly because my wiki is only accessed from one menu item called My Wiki.
The in_place_editing plugin uses the following ajax command:
I have now completed the task for this workshop. My next post will be some screenshots of what I have ended up with.
No comments:
Post a Comment