Learning Notes for 12in12 Week 1
Dec 22 nd , 2014 5:17 pm
CREATE OUR NEW RAILS APPLICATION
1
2
3
4
5
$ rails new reddit
$ cd reddit
$ git init
$ git add .
$ git commit -m "reddit init"
LINK SUBMISSIONS
1
2
3
4
5
6
7
$ git checkout -b link_scaffold
$ rails g scaffold link title:string url:string
$ rake db:migrate
$ git add .
$ git commit -m "Generate Links Scafffold"
$ git checkout master
$ git merge link_scaffold
1
2
3
$ git checkout -b add_users
$ bundle install # after add devise into Gemfile
$ rails g devise:install
then modify config/env…/deve… for mail
add flash in app/views/layouts/application.html.erb
app/views/layouts/application.html.erb 1
2
3
< % flash.each do | name , msg | %>
<%= content_tag :div, msg, class: "alert alert- #{ name } " %>
< % end %>
USERS
add devise gem 1
2
3
$ rails g devise:views
$ rails g devise User
$ rake db:migrate
add devise gem 1
2
3
$ git status
$ git add .
$ git commit -am "Add devise and create User model"
app/views/links/index.html.erb 1
2
3
4
5
6
7
8
9
10
11
12
< % if user_signed_in? %>
<ul>
< li >< %= link_to 'Submit link', new_link_path %></li>
<li><%= link_to 'Account' , edit_user_registration_path %></li>
< li >< %= link_to 'Sign out', destroy_user_session_path, :method = > :delete %></li>
< /ul>
<% else %>
<ul>
<li><%= link_to 'Sign up', new_user_registration_path %></ li >
< li ><%= link_to 'Sign in' , new_user_session_path %></li>
< /ul>
<% end %>
app/models 1
2
3
4
5
# app/models/user.rb
has_many :links
# app/models/link.rb
belongs_to :user
app/models 1
2
$ rails g migration add_user_id_to_links user_id : integer : index
$ rake db : migrate
app/models 1
2
3
$ git status
$ git add .
$ git commit - am "Add association between Link and User"
app/controllers/links_controller.rb 1
2
3
4
5
6
7
8
9
10
before_filter :authenticate_user! , :except => [ :index , :show ]
. . .
def new
@link = current_user . links . build
end
def create
@link = current_user . build ( link_params )
. . .
end
app/views/links/index.html.erb 1
2
3
4
< % if link . user == current_user %>
<td> < %= link_to 'Edit', edit_link_path(link) %></td>
<td><%= link_to 'Destroy' , link , method : :delete , data : { confirm : 'Are you sure?' } %></td>
< % end %>
app/controllers/links_controller.rb 1
2
3
4
5
6
7
8
9
10
. . .
before_action :authorized_user , only : [ :edit , :update , :destroy ]
. . .
private
. . .
def authorized_user
@link = current_user . links . find_by ( id : params [ :id ] )
redirect_to links_path , notice : "Not authorized to edit this link" if @link . nil?
end
app/controllers/links_controller.rb 1
2
3
4
$ git status
$ git commit - am "Authorization on links"
$ git checkout master
$ git merge add_users
STRUCTURE AND SOME STYLING