Created
November 3, 2015 19:28
-
-
Save awongh/428fa5090fd7dbfa4788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## User Auth Rails app: What we're adding: | |
- User model | |
```bash | |
rails g model user username:string password_hash:string | |
``` | |
- user_id foreign key in tables (user has many cars) | |
generate a migration | |
```bash | |
rails generate migration addUserId | |
``` | |
add the foreign key to the cars table | |
```ruby | |
class AddUserId < ActiveRecord::Migration | |
def change | |
add_reference :cars, :user | |
end | |
end | |
``` | |
- a login form | |
this requires some routes: | |
```ruby | |
get 'login' => 'sessions#new' | |
post 'login' => 'sessions#create' | |
delete 'logout' => 'sessions#destroy' | |
``` | |
```ruby | |
<%= form_for(:session, url: login_path) do |f| %> | |
<%= f.label :username %> | |
<%= f.text_field :username %> | |
<%= f.label :password %> | |
<%= f.password_field :password %> | |
<%= f.submit "log in"%> | |
<% end %> | |
``` | |
- a register form | |
```ruby | |
post 'login' => 'sessions#create' | |
``` | |
```ruby | |
<%= form_for(:user, url: login_path) do |f| %> | |
<%= f.label :username %> | |
<%= f.text_field :username %> | |
<%= f.label :password %> | |
<%= f.password_field :password %> | |
<%= f.label :password_confirmation %> | |
<%= f.password_field :password_confirmation %> | |
<%= f.submit "log in"%> | |
<% end %> | |
``` | |
- an edit user form |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment