skip to main |
skip to sidebar
Workshop 5: Part C: Screen layouts and forms processing with text fields, check boxes, radio buttons and multiple list controls
- Create a new application called cabs in the same projects directory to demonstrate the use of an active view.
> rails cabs
> cd cabs
Complete
- Create a controller called Vehicle in cabs\app\controllers
cabs> ruby script/generate controller Vehicle
Complete
- Add an action to vehicle_controller.rb as the method called cabtype
class VehicleController< ApplicationController
def cabtype
end
end
Complete
- 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.
Complete
- Save the view and restart the Web server and navigate to http://localhost:3000/cabs/cabtype
Navigated to http://127.0.0.1:3004/vehicle/cabtype
- Create a file in the public directory - \cabs\public called input.html
Done this
- 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
Done this
- Edit the view template cabtype.rhtml
Done this
- Start the Web server and go to the opening page of this application at http://localhost:3000/input.html
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.
- Submit the forms data. What do you find?
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