Showing posts with label Workshop 5. Show all posts
Showing posts with label Workshop 5. Show all posts

Monday, July 6, 2009

Workshop 5/6: More Taxi booking system - followup

Must give thanks to Karen's comment. Even though what she said to do was what I had done, the fact that she commented actually triggered the correct outcome for me.

I have got the BookCab form integrated with my OTBS. In order to do this I had to do the following

  1. Move the bookcab method into the VehicleController
  2. Move the bookcab.html.erb file into the views\vehicle\ directory
  3. Change the form action to form_action ="cabtype" instead of "vehicle/cabtype"
  4. modify my application.html.erb file so that the 'Book Cab' link pointed to the correct controller and action.

On the face of it it looks like the variable scope is confined to the MVC that is of interest. In this case it was the cab booking variables in the bookcab view being transferred to the cabtype view.

My next step now is to introduce a login mechanism and enable passenger information to be collected.

Sunday, July 5, 2009

Workshop 5/6: More Taxi booking system

I have just been having a lot of fun.

I thought I would go back into the Taxi Booking system, to improve the look of the site and to look at adding login functionality. This would involve utilising what we had developed in workshop 4 with the changes in workshop 5.

I also wanted to change how I had dealt with the protect_from_forgery error that we all got in workshop 5. I simply hashed out the line, and so everything worked.

Well the idea started out as a good one. Unfortunately, I managed to break everything, so that nothing worked. I changed the routes file so that there were no recognised routes.

Scratching my head for half an hour ensued, until I realised that I had in actual fact hashed out all the route info in the routing file. So message actually made sense. With that sorted out and the taxi details showing again I decided to press on.

My Online Taxi Booking System now looks like

Unfortunately the information doesn't came across as you can see by the following:

I am beating my head against a brick wall at the moment. Possibly mind gone blank syndrome. I definitely need to get back to RailsSpace, for another installment, which might clear things up a bit. In the meantime, I close this blog post with a screenshot of my pages_controller and file structure

You may notice that the Vehicle Controller is empty. I was getting some routing issues and so just moved the code into the pages controller. I am definitely thinking this was the wrong way to go. But that is where it is at the moment.

Update

Well after posting this blog, I had a slight rethink and put the Cabtype method back into the vehicle controller. The views all seem to work, and the cabtype view does get displayed after submitting the booking form. Alas the variables are not being initialised, so the cabtype view is not displaying the selected information. Any ideas anyone?

Tuesday, June 23, 2009

Workshop 5: Part C: Screen layouts and forms processing with text fields, check boxes, radio buttons and multiple list controls

  1. Create a new application called cabs in the same projects directory to demonstrate the use of an active view. > rails cabs > cd cabs
  2. Complete
  3. Create a controller called Vehicle in cabs\app\controllers cabs> ruby script/generate controller Vehicle
  4. Complete
  5. Add an action to vehicle_controller.rb as the method called cabtype class VehicleController< ApplicationController def cabtype end end
  6. Complete
  7. Add a view template - cabs\app\views\vehicle\cabtype.rhtml We will edit this view in later steps but you may like to add your own test HTML code to the view at this stage.
  8. Complete
  9. Save the view and restart the Web server and navigate to http://localhost:3000/cabs/cabtype
  10. Navigated to http://127.0.0.1:3004/vehicle/cabtype
  11. Create a file in the public directory - \cabs\public called input.html
  12. Done this
  13. Edit the vehicle_controller.rb here is a start. The data in each form element in the Rails application can be accessed via its name and a hash called params class VehicleController< ApplicationController def cabtype @data1 = params[:text1] @data2 = params[:check1] @data3 = params[:radios1] @data4 = params[:building1] end end
  14. Done this
  15. Edit the view template cabtype.rhtml
  16. Done this
  17. Start the Web server and go to the opening page of this application at http://localhost:3000/input.html
  18. Did this and had a horrible looking screen. If you just do a cut and paste from a word file the quotes play up just a little bit. This leads to a horrible looking screen rendering. Having done this Craigs mention of this problem comes to light. Should have been a touch more careful, and validated what i copied a bit more. But lesson hopefully learnt. Took but a matter of moments to rectify and have the screen render nicely.
  19. Submit the forms data. What do you find?
  20. I get a rather horrible screen that screams ActionController::InvalidAuthenticityToken at me. This is obviously security related. Looked in Application Controller and noticed the second line "protect_from_forgery".
    Hashed this out.
    Kept getting response, No route matches "/cabtype" with {:method=>:post}
    This led me to change the Submit action on the input form to "vehicle/cabtype"
    Unfortunately this still did not work. After a few minutes of scratching my head I did the brute force approach and recreated the project. Here was the whole COC conversation that happened on the forums all over again. But brute force worked.

Workshop 5:Part B: The active view: passing data from an action too a view

  1. Create a new application called scenery in the same projects directory to demonstrate the use of an active view. > rails scenery > cd scenery
  2. Complete
  3. Create a controller called Demo in scenery\app\controllers scenery> ruby script/generate controller Demo
  4. Complete

  5. Add an action to demo_controller.rb as the method called rubycobe class DemoController< ApplicationController def rubycode end end
  6. Complete
  7. Add a view template - scenery\app\views\demo\rubycode.rhtml We will edit this view in later steps but you may like to add your own test HTML code to the view at this stage.
  8. Complete
  9. Save and restart the Web server and navigate to http://localhost:3000/scenery/rubycode
  10. If I was to follow the script blindly then one of two things will happen here.
    • I have made a mistake or
    • the workshop script is wrong

    If I navigate to the url
    ../scenery/rubycode
    I get the error Routing Error.
    However if I do the logical thing and navigate to
    ../demo/rubycode
    then I get my expected page returned.
  11. Use the Time.now example to pass data from an action to a view.

  12. Modify and save the rubycode action with a value for the time instance variable in the DemoController class in app\controllers\demo_controller.rb class DemoController< ApplicationController def rubycode @time_now = Time.now end end
  13. Done that
  14. Then modify and save the corresponding view template in \app\views\demo\rubycode.rhtml by adding a call by reference to the action’s instance variable: <_br> The time is <_%= @time.now %_> <_br>
  15. Obviously this piece of code is wrong. We should be using the variable defined in the controller @time_now
  16. Restart the Web server and navigate the browser to http://localhost:3000/demo/rubycode Data has been passed from the action to the view as it is done with SQL requests. The instance variables of a Ruby class are available to view templates by referencing the action’s instance variables by name in the view .rhtml template.
  17. The output and the code files are shown below

Workshop 5: Part A: Viewing the action

  1. Create the Rails application framework in the projects folder: C:\InstantRails\...\projects\>rails animals

  2. Complete
  3. Running the application on localhost:3000 using the WeBrick ruby server (or Mongrel as alternative) and access via Web browser at http://localhost:3000/

  4. Create the controller to make the application do an action. This is under the controller-action/model-view structure. Stop the WEBrick server each time you edit Ruby classes and then re-start or refresh the views you are testing. Use the Ruby command below: >ruby script/generate controller Mammal The mammal_controller.rb contains just a bare class description: class MammalController< ApplicationController end and the ApplicationController class inherits from ActionController::Base class in the ActionController module under Rails.

  5. Complete
  6. Test the controller by starting the WEBrick server and navaigatibng the browser to http://localhost:3000/mammal Note how the controller name is appended to the end of the URL and that no action resulted because there are no controller methods.

  7. I get the following:
    Routing Error No route matches "/mammals" with {:method=>:get}
    I think this is simply because I coulnt follow instructions and typed /mammals rather than /mammal.
  8. Create an action by editing and saving the mammal_controller.rb class in projects\animals\app\controllers using your text editor to add the method below: class MammalController< ApplicationController def breathe end end

  9. Complete
  10. Start the WEBrick server and browse at http://localhost:3000/mammals/breathe where you will get a “missing template” message since it is missing a view for the breathe method. Rails is trying to connect the breathe method action of the mammal controller to a view, by using the action’s name – breathe. This view template is created as breathe.rhtml and stored in the \projects\animals\views\mammal directory.

  11. Create and save a view in that directory by using a text editor to create a view called breathe.rhtml <_html> <_head> <_title>Breathe Easy <_body>Inhale and Exhale Restart the WEBrick serve r and browse again at http://localhost:3000/mammals/breathe

  12. Browsed to ../mammal/ and got the following message
    Unknown action No action responded to index. Actions: breathe
    So added the action /mammal/breathe and got the expected result.
  13. Try Ruby code and HTML in the action view by using the <%....%> wrapper around the inserted Ruby code. Here are some snippets to try from workshop 4:
    5 + 6 =<%= 5 + 6 %>

    =<% 4.times do %> Inhale Exhale
    <%end%>
    Time is <%=Time.now %>
    The output of this little exercise is

    NOTE: in practise you normally perform calculations in the action (method) and pass the results to the view.

Workshop 5: Admiring the scenery

Topic objectives
  • To reflect on what has been done so far with Ruby on Rails
  • To reflect on my role and decide to continue as EITHER a Developer OR as an IT infrastructure manager
I will be continuing the workshops in the role of a developer. So I will be on the RED team.