Intro to Gems / More About Forms
Today's Topics
- Introduction to Rails Gems
- Ruby Gems
- Bundler
- Gemfile/Gemfile.lock
- RVM Gemsets
- More About Forms
- Validations
- Simple Form Gem
What is a gem?
- a. a type of rock
- b. Girls Education & Mentoring Services
- c. something to do with rails....
- d. the command to download ruby libraries
Rails Gems
- Gems are libraries written for the Ruby language
- Gems: Ruby, Modules: Python, Packages: JS
- Gems have unique names and versions
- Gems include:
- Code (with tests)
- Documentation
- Gemspec (contains information about a gem)
- There is literally a gem for everything
- Emails, payments, easier developing, etc.
Guess what rails is?
Bundler
- Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed
- Without bundler:
- gem install pg
- With bundler:
- all dependencies stored in Gemfile
- bundle install installs all
dependencies listed in Gemfile
- bundler is a gem!
Gemfile
- Gemfiles require at least one gem source, in the form of the URL for a Rubygems server (https://rubygems.org)
- Git repositories are also valid gem sources, as long as the repo contains one or more valid gems
- Dependencies can be placed in groups
Gemfile.lock
- After running bundle install, bundler keeps a list of all gem names and versions in a file called Gemfile.lock
- Gemfile.lock ensures that the same dependencies are called across different environments
- Think of Gemfile.lock as a snapshot of all of the gems and their versions
RVM
- Remember RVM?
- In the first week, we asked you to set up rails, which included this line:
- \curl -sSL https://get.rvm.io | bash -s stable --ruby=2.1.2
RVM Gemsets
- RVM is a tool that allows you to install multiple versions of Ruby on a machine, and allows you to switch between them
- RVM has gemsets: which are a set of gems specific to a given project
- Why use gemsets?
- faster loading, because less gem-specs need to be parsed
- easy to distribute and share
Gem Example/Time!
- We will now introduce a gem for class
- We will build off of previously covered material and try to make implementing things easier
Survey
- How many people remember what we talked about for forms last week?
- How many people did last week's lab, Lab 4: the pokemon app in rails?
Logistics
- HW1 is in the process of being graded
- hopefully we will release grades and comments by Monday
- Proj 1 will be coming out this weekend
- Look on Piazza for when its released
- Due October 26
Forms cont.
- Last week, we learned about basic forms and CRUD
- Today, we are going to talk about validating forms
- Validating a form ensures that only valid data is saved into your database
Lab 4 Throwback!
- Remember last week's Pokemon lab? Let's spend a little more time looking at it
- What happens when trying to submit an empty name and description for a Pokemon and submitting?
- a. The server throws an error
- b. The object is created successfully, but you can never find it....
- c. Ash Ketchum lectures you on treating your Pokemon with respect
- d. The "" Pokemon is mute and can never show its excitement, sadness, etc.
Validations
- Where should we validate forms?
- There are several options
- model-level validations
- client-side validations
- controller-level validations
- Let's find these different options in our lab!
- There are several options
Validations
- Today in class we will cover several types of model validations
- Presence
- Length
- Format (Regex)
- For more on regex, see http://rubular.com/
Demo
Error Messages: Flash
- The flash is a special part of the session which is cleared with each request
- Represented as a hash
- Useful for passing error messages
Demo
Simple Forms
- "Simple Form aims to be as flexible as possible while helping you with powerful components to create your forms. The basic goal of Simple Form is to not touch your way of defining the layout, letting you find the better design for your eyes."
- For full documentation:
- https://github.com/plataformatec/simple_form
Simple Forms
- Simple Forms handles a lot of things for you that we just implemented
- Simple forms has built-in error handling and other features that extends form_for's functionality
Simple Form Gem Installlation
- Add it to your Gemfile
- gem 'simple_form'
- Run bundle install to install all gems in Gemfile, including simple_form
- Run the generator
- rails generate simple_form:install
Questions?
Work Time
- http://bit.ly/1xHQLXH
- We will reimplement what we did in lab 4, but this time, using simple forms
Week 5 - Gems and simple_form
By Rails Decal
Week 5 - Gems and simple_form
Fifth Lecture for the Rails Decal at UC Berkeley
- 2,209