In this lab, you'll practice building forms in Rails by creating a basketball team sign-up sheet. Your application will use Rails form helpers with proper RESTful routing conventions, and will display the data from the form after it has been submitted by the user.
-
Run
bundle install -
Run
bin/rails server -
Set up your routes in
config/routes.rb:- Use the
resourcesmethod to create RESTful routes for teams - This will automatically create routes for
new,create,show, etc.
- Use the
-
Create controller actions in
app/controllers/teams_controller.rb:- Add a
newaction to display the form (should set up an@teaminstance variable) - Add a
createaction to process the form submission and redirect to the show action - Add a
showaction to display the team information
- Add a
-
Build the form in
app/views/teams/new.html.erb:- Use Rails
form_withhelper with the@teammodel object - The form will automatically submit to the correct RESTful endpoint
- Add fields for: Team name ('name'), Coach ('coach'), Point Guard ('pg'), Shooting Guard ('sg'), Power Forward ('pf'), Small Forward ('sf'), Center ('c')
- Include a submit button (any text or styling is fine)
- The form fields should use nested naming (e.g.,
team[name],team[coach], etc.)
- Use Rails
-
Display the results in
app/views/teams/show.html.erb:- Show all the submitted team information
- Display each field with labels like "Team Name: [name]", "Coach: [coach]", etc.
- Use the
@teaminstance variable passed from your controller
Run bundle exec rspec to test your implementation. Make sure all tests pass!
The tests will check that:
- Your routes are set up correctly using RESTful conventions
- Your form has all the required fields with proper nested naming
- Your form submits to the correct RESTful endpoint
- Your show page displays all the submitted data correctly
- RESTful routing: Using
resources :teamsfor standard CRUD operations - Form helpers: Using
form_withwith model objects for automatic routing - Nested parameters: Form fields using
team[attribute_name]format - Controller actions: Following the standard
new,create,showpattern - Redirects: Redirecting after successful form submission to the show page