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.

No comments:

Post a Comment