-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
494 additions
and
29 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class RelationshipsController < ApplicationController | ||
before_filter :signed_in_user | ||
|
||
def create | ||
@user = User.find(params[:relationship][:followed_id]) | ||
current_user.follow!(@user) | ||
respond_to do |format| | ||
format.html { redirect_to @user } | ||
format.js | ||
end | ||
end | ||
|
||
def destroy | ||
@user = Relationship.find(params[:id]).followed | ||
current_user.unfollow!(@user) | ||
respond_to do |format| | ||
format.html { redirect_to @user } | ||
format.js | ||
end | ||
end | ||
end |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class Relationship < ActiveRecord::Base | ||
attr_accessible :followed_id | ||
|
||
belongs_to :follower, class_name: "User" | ||
belongs_to :followed, class_name: "User" | ||
|
||
validates :follower_id, presence: true | ||
validates :followed_id, presence: true | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$("#follow_form").html("<%= escape_javascript(render('users/unfollow')) %>") | ||
$("#followers").html('<%= @user.followers.count %>') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
$("#follow_form").html("<%= escape_javascript(render('users/follow')) %>") | ||
$("#followers").html('<%= @user.followers.count %>') |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<% @user ||= current_user %> | ||
<div class="stats"> | ||
<a href="<%= following_user_path(@user) %>"> | ||
<strong id="following" class="stat"> | ||
<%= @user.followed_users.count %> | ||
</strong> | ||
following | ||
</a> | ||
<a href="<%= followers_user_path(@user) %>"> | ||
<strong id="followers" class="stat"> | ||
<%= @user.followers.count %> | ||
</strong> | ||
followers | ||
</a> | ||
</div> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= form_for(current_user.relationships.build(followed_id: @user.id), | ||
remote: true) do |f| %> | ||
<div><%= f.hidden_field :followed_id %></div> | ||
<%= f.submit "Follow", :class => "btn btn-large btn-primary" %> | ||
<% end %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<% unless current_user?(@user) %> | ||
<div id="follow_form"> | ||
<% if current_user.following?(@user) %> | ||
<%= render 'unfollow' %> | ||
<% else %> | ||
<%= render 'follow' %> | ||
<% end %> | ||
</div> | ||
<% end %> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<%= form_for(current_user.relationships.find_by_followed_id(@user), | ||
html: { method: :delete }, | ||
remote: true) do |f| %> | ||
<%= f.submit "Unfollow", :class => "btn btn-large" %> | ||
<% end %> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<div class="row"> | ||
<aside class="span4"> | ||
<section> | ||
<%= gravatar_for @user %> | ||
<h1><%= @user.name %></h1> | ||
<span><%= link_to "view my profile", @user %></span> | ||
<span><b>Microposts:</b> <%= @user.microposts.count %></span> | ||
</section> | ||
<section> | ||
<%= render 'shared/stats' %> | ||
<% unless @users.empty? %> | ||
<div class="user_avatars"> | ||
<% @users.each do |user| %> | ||
<%= link_to gravatar_for(user, :size => 30), user %> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
</section> | ||
</aside> | ||
<div class="span8"> | ||
<h3><%= yield(:title) %></h3> | ||
<% if @users.any? %> | ||
<ul class="users"> | ||
<%= render @users %> | ||
</ul> | ||
<%= will_paginate @users %> | ||
<% end %> | ||
</div> | ||
</div> |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class CreateRelationships < ActiveRecord::Migration | ||
def change | ||
create_table :relationships do |t| | ||
t.integer :follower_id | ||
t.integer :followed_id | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :relationships, :follower_id | ||
add_index :relationships, :followed_id | ||
add_index :relationships, [:follower_id, :followed_id], unique: true | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,42 @@ namespace :db do | |
desc "Fill database with sample data" | ||
task populate: :environment do | ||
Rake::Task['db:reset'].invoke | ||
admin = User.create!(name: "Example User", | ||
email: "[email protected]", | ||
password: "foobar", | ||
password_confirmation: "foobar") | ||
admin.toggle!(:admin) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
password = "password" | ||
User.create!(name: name, | ||
email: email, | ||
password: password, | ||
password_confirmation: password) | ||
end | ||
make_users | ||
make_microposts | ||
make_relationships | ||
end | ||
end | ||
|
||
def make_users | ||
admin = User.create!(name: "Example User", | ||
email: "[email protected]", | ||
password: "foobar", | ||
password_confirmation: "foobar") | ||
admin.toggle!(:admin) | ||
99.times do |n| | ||
name = Faker::Name.name | ||
email = "example-#{n+1}@railstutorial.org" | ||
password = "password" | ||
User.create!(name: name, | ||
email: email, | ||
password: password, | ||
password_confirmation: password) | ||
end | ||
end | ||
|
||
users = User.all(limit: 6) | ||
50.times do | ||
content = Faker::Lorem.sentence(5) | ||
users.each { |user| user.microposts.create!(content: content) } | ||
end | ||
def make_microposts | ||
users = User.all(limit: 6) | ||
50.times do | ||
content = Faker::Lorem.sentence(5) | ||
users.each { |user| user.microposts.create!(content: content) } | ||
end | ||
end | ||
|
||
def make_relationships | ||
users = User.all | ||
user = users.first | ||
followed_users = users[2..50] | ||
followers = users[3..40] | ||
followed_users.each { |followed| user.follow!(followed) } | ||
followers.each { |follower| follower.follow!(user) } | ||
end |
Oops, something went wrong.
8a0eaeb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Michael.
Don't you think you should add :destroy in before_filter :signed_in_user, only: [:index, :edit, :update] array, as you did in the first sample_app?
Otherwise, only :admin_user filter will be called and that will call current_user.admin? in return , which is an error of " undefined method `admin?' for nil:NilClass ". Do you think this error could be disregarded?
Thanks for your generosity.
8a0eaeb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error you mention should never happen, because only signed-in users can see the 'delete' links. For completeness, though, I agree that
:destroy
should be included in thesigned_in_user
before filter, and I've amended the tutorial accordingly. Thanks!