Lab for Lecture 3 that goes over models, db migrations, rails console, and CRUD
For this lab you will be adding additional features to the current application included.
- cd into lab3railsapp.
- Run 'rake db:migrate'
- Run 'rails s'
- go to the url /users
You should be able to do this fine. If you run into any issues, please see a TA for help. If you see no users, it means you need to create some first
- Go to your rails console, and add 3 users
- Make sure that you can see these 3 users at /users
- Use the console to edit the names of these users
- See the changes appear at /users
- Delete the age field from the user
- Running 'User' on rails console should not show the age field
- Add it back, but this time call it "yearsold" (Update the edit form and the controller as well)
- Running 'User' on rails console should now show the yearsold field.
- Go ahead and update all the users again using the console, since their yearsold will be blank now
- Show only users that are > 50 yearsold on the index
- Add a create form so you can go to /users/new to create a user successfully.
In console:
> User.all # This will show all users
> wonjun = User.new
> wonjun.age = 99
> wonjun.name = "wonjun"
> wonjun.save
> User.all # You can now see wonjun
Find a user in the console by using User.find() Remember to create a variable with them and then save it to the database to actually edit!
- migrations (also help you delete :D)
$ rails generate migration RemoveFieldNameFromTableName field_name:datatype
$ rake db:migrate
# If you can't remember a command or need to know, google is your friend!
- migrations
- Checkout the index function
Hint: You need to use a form, and work with both the new/create functions in the controller Check out https://www.railstutorial.org/book to learn more about how you could do this