Ruby On Rails Classroom
Neha Jaggi /
Professional /
Web Technology
- Foreword to the First Edition
- Acknowledgments
- About the Author
- From Zero to Deploy
- Development Environments
- Ruby RubyGems Rails and Git
- The First Application
- rails server
- Model-view-controller MVC
- Version Control with Git
- What Good Does Git Do You
- GitHub
- Branch Edit Commit Merge
- Deploying
- Heroku Setup
- Conclusion
- A Demo App
- Planning the Application
- Modeling Demo Users
- Modeling Demo Microposts
- The Users Resource
- A User Tour
- MVC in Action
- Weaknesses of this Users Resource
- The Microposts Resource
- A Micropost Microtour
- Putting the micro in Microposts
- A User has many Microposts
- Inheritance Hierarchies
- Deploying the Demo App
- Conclusion
- Static Page
- Mostly Static Pages
- Truly Static Pages
- Static Pages with Rails
- Our First Tests
- Test-driven Development
- Adding a Page
- Testing a Title Change
- Passing Title Tests
- Embedded Ruby
- Eliminating Duplication with Layouts
- Conclusion
- Advanced Setup
- Eliminating bundle exec
- Automated Tests with Guard
- Speeding up Tests with Spork
- Tests inside Sublime Text
- Rails-Flavored Ruby
- Strings and Methods
- Objects and Message Passing
- Method Definitions
- Other Data Structures
- Blocks
- Hashes and Symbols
- CSS revisited
- Ruby Classes-Constructors
- Class Inheritance
- Modifying Built-in Classes -A Controller Class
- A User Class
- Conclusion-Exercises
- Adding Some Structure
- Site Navigation
- Bootstrap and Custom CSS
- Partials
- Sass and the Asset Pipeline
- Syntactically Awesome Stylesheets
- Layout Links
- Route Tests
- Rails Routes
- Named Routes
- Pretty RSpec
- User Signup A First Step
- Signup URI
- Conclusion-Exercises
- Modeling Users
- User Model
- Database Migrations
- The Model File
- Creating User Objects
- Finding User Objects
- Updating User Objects
- User Validations
- Validating Presence
- Length Validation-Format Validation
- Uniqueness Validation
- Adding a Secure Password
- An Encrypted Password
- Password and Confirmation
- User Authentication
- User Has Secure Password
- Creating a User
- Conclusion-Exercises
- Showing Users
- Debug and Rails Environments
- A Users Resource
- Testing the User Show Page with Factories
- A Gravatar Image and a Sidebar
- Signup Form
- Tests for User Signup
- Using form for
- The Form HTML
- Signup Failure
- Signup Error Messages
- The Finished Signup Form
- The Flash
- Deploying to Production with SSL
- Conclusion-Exercises
- Sessions and Signin Failure
- Sessions Controller
- Signin Tests
- Signin Form
- Reviewing Form Submission
- Rendering with a Flash Message
- Signin Success
- Remember Me
- A Working sign in Method
- Current User
- Changing the Layout Links
- Signin upon Signup
- Signing Out
- Introduction to Cucumber Optional
- Installation and Setup
- Features and Steps
- Counterpoint RSpec Custom Matchers
- Conclusion-Exercises
- Updating Users
- Edit Form
- Unsuccessful Edits
- Successful Edits
- Authorization
- Requiring Signed-in Users
- Requiring the Right User
- Friendly Forwarding
- Showing All Users
- User Index
- Sample Users
- Pagination
- Partial Refactoring
- Deleting Users-Administrative Users
- The destroy Action
- Conclusion-Exercises
- A Micropost Model
- The Basic Model
- Accessible Attributes and the First Validation
- User Micropost Associations
- Micropost Refinements
- Content Validations
- Showing Microposts
- Augmenting the User Show Page
- Sample Microposts
- Manipulating Microposts
- Access Control
- Creating Microposts
- A Proto-feed
- Destroying Microposts
- Conclusion-Exercises
- Following Users
- The Relationship Model
- A Problem with the Data Model and a Solution
- User Relationship Associations
- Validations
- Followed users
- Followers
- Sample Following Data
- Stats and a Follow Form
- Following and Followers Pages
- A Working Follow Button the Standard Way
- A Working Follow Button with Ajax
- Making the output of find readable in shell
- CRUD Operation-Create
- The Status Feed
- Motivation and Strategy
- A First Feed Implementation
- Subselects
- The New Status Feed
- Conclusion
- Exercises
Ruby On Rails Lesson
Database Migrations
You may recall from Section 4.4.5 that we have already encountered, via a custom-built User class, user objects with name and email attributes. That class served as a useful example, but it lacked the critical property of persistence: When we created a User object at the Rails console, it disappeared as soon as we exited. Our goal in this section is to create a model for users that won’t disappear quite so easily.
As with the User class in Section 4.4.5, we’ll start by modeling a user with two attributes, a name and an email address, the latter of which we’ll use as a unique username.3 (We’ll add an attribute for passwords in Section 6.3.) In Listing 4.9, we did this with Ruby’s attr_accessor method:
attr accessor :name, :email
.
.
.
end
In contrast, when using Rails to model users, we don’t need to identify the attributes explicitly. As noted briefly above, to store data Rails uses a relational database by default, which consists of tables composed of data rows, where each row has columns of data attributes. For example, to store users with names and email addresses, we’ll create a users table with name and email columns (with each row corresponding to one user). By naming the columns in this way, we’ll let Active Record figure out the User object attributes for us.
Let’s see how this works. (If this discussion gets too abstract for your taste, be patient; the console examples starting in Section 6.1.3 and the database browser screenshots in Figure 6.3 and Figure 6.6 should makethings clearer.) You may recall from Listing 5.28 that we created a Users controller (along with a new action) using the command
There is an analogous command for making a model: generate model. Listing 6.1 shows the command to generate a User model with two attributes, name and email.
(Note that, in contrast to the plural convention for controller names, model names are singular: a Users controller, but a User model.) By passing the optional parameters name:string and email:string, we tell Rails about the two attributes we want, along with what types those attributes should be (in this case, string). Compare this with including the action names in Listing 3.4 and Listing 5.28.
One of the results of the generate command in Listing 6.1 is a new file called a migration. Migrations provide a way to alter the structure of the database incrementally, so that our data model can adapt to changing requirements. In the case of the User model, the migration is created automatically by the model generation script; it creates a users table with two columns, name and email, as shown in Listing 6.2. (We’ll seein Section 6.2.5 and again in Section 6.3 how to make a migration from scratch.)
Neha Jaggi
Skills Ruby On Rails
Qualifications :- High School - , College/University - Graphic Era Hill University, Dehradun, College/University - ,Location :-Dehradun,Dehradun,UTTARAKHAND,India
Description:-
Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Skilled in Web Technologies (Ruby on Rails, PostgreSQL, php, Laravel and AJAX).
Explore