These projects will give you a chance to actually build some forms, both using nearly-pure HTML and then graduating to using the helper methods that Rails provides.
In this project, you’ll build a form the old fashioned way and then the Rails way.
-
Build a new Rails app:
- Build a new Rails app (called “re-former”):
rails new re-former
- Create a new GitHub repo and connect the remote to your local git repo. Check in and commit the initial stuff.
- Modify your README file to say something you’ll remember later.
- Build a new Rails app (called “re-former”):
-
Create and migrate a User model:
- Create and migrate a User model with :username, :email, and :password:
rails generate model User username:string email:string password:string rails db:migrate
- Add validations for presence to each field in the model.
- Create the :users resource in your routes file.
- Create and migrate a User model with :username, :email, and :password:
-
Build UsersController:
- Write empty methods for #new and #create in your UsersController:
rails generate controller Users new create
- Create your #new view in app/views/users/new.html.erb.
- Write empty methods for #new and #create in your UsersController:
-
HTML form:
- Build a form for creating a new user in app/views/users/new.html.erb.
-
Railsy forms with #form_tag:
- Comment out your entire HTML form.
- Convert your tag to use a #form_tag helper and all of your inputs into the proper helper tags via #*_tag methods.
-
Railsy-er forms with #form_with:
- Modify your #new action in the controller to instantiate a blank User object and store it in an instance variable called @user.
- Rebuild the form using #form_with and the @user from your controller.
-
Editing:
- Update your routes and controller to handle editing an existing user.
- Create the Edit view at app/views/users/edit.html.erb and copy/paste your form from the New view.
-
Save to Git and GitHub:
- Initialize a git repository in your project folder if you haven't already:
git init
- Add all files to the staging area:
git add . - Commit the changes:
git commit -m "Initial commit" - Create a new repository on GitHub.
- Push your local repository to GitHub:
git remote add origin https://github.com/USERNAME/Forms.git git branch -M main git push -u origin main
- Initialize a git repository in your project folder if you haven't already:
To interact with the application via the Rails console:
-
Open the Rails console:
rails console
-
Create a new user:
user = User.new(username: "example_user", email: "user@example.com", password: "password123") user.save
-
Find existing users:
users = User.all
-
Update user attributes:
user = User.find_by(username: "example_user") user.update(email: "new_email@example.com")
-
Delete a user:
user = User.find_by(username: "example_user") user.destroy
- Rails Guides: Form Helpers