The code we're gonna use is at https://github.com/mkwiatkowski/rails4-with-devise-seed
$ git clone https://github.com/mkwiatkowski/rails4-with-devise-seed.git twitter
$ cd twitter
$ bundle
$ rake db:setup
$ rails s
$ rails g controller tweets
app/controllers/tweets_controller.rb
class TweetsController < ApplicationController
def new
end
end
config/routes.rb
resources :tweets
$ rake routes
<%= link_to "New tweet", new_tweet_path %>
$ rails g model tweet content:text user:references
$ rake db:migrate
app/models/user.rb
has_many :tweets
app/views/tweets/new.html.erb
<%= form_for @tweet do |f| %>
<%= f.text_field :content, required: true %>
<%= f.submit "Tweet" %>
<% end %>
app/controllers/tweets_controller.rb
@tweet = current_user.tweets.new
app/controllers/tweets_controller.rb
def create
end
app/controllers/tweets_controller.rb
current_user.tweets.create(params.require(:tweet).permit(:content))
flash[:notice] = "Tweet added"
redirect_to new_tweet_path
app/views/layouts/application.html.erb
<%= link_to "Your tweets", tweets_path %>
app/controllers/tweets_controller.rb
def index
@tweets = current_user.tweets
end
app/views/tweets/index.html.erb
<ul>
<% @tweets.each do |tweet| %>
<li>
<%= tweet.content %>
</li>
<% end %>
</ul>
app/views/tweets/index.html.erb
<small><%= tweet.created_at %></small>
app/views/tweets/index.html.erb
<small><%= time_ago_in_words(tweet.created_at) %> ago</small>
app/views/tweets/index.html.erb
<%= link_to "New tweet", new_tweet_path %>
app/controllers/tweets_controller.rb
redirect_to tweets_path
app/controllers/tweets_controller.rb
@tweets = current_user.tweets.order(created_at: :desc)
app/views/layouts/application.html.erb
<p>
<% if current_user %>
<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<%= link_to "Your tweets", tweets_path %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %>
<% end %>
</p>
app/views/layouts/application.html.erb
<%= link_to "Sign in", new_user_session_path %>
Gemfile
gem 'bootstrap-sass', '~> 3.0.0'
$ bundle
app/assets/stylesheets/application.css
= require bootstrap
app/views/layouts/application.html.erb
<nav class="navbar navbar-default" role="navigation">
<div class="collapse navbar-collapse">
<ul class="nav nav-pills">
<% if current_user %>
<li class="<%= 'active' if current_page?(root_path) %>"><%= link_to "Home", root_path %></li>
<li class="<%= 'active' if current_page?(tweets_path) %>"><%= link_to "Your tweets", tweets_path %></li>
<li class="<%= 'active' if current_page?(new_tweet_path) %>"><%= link_to "New tweet", new_tweet_path %></li>
<li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% else %>
<li class="<%= 'active' if current_page?(new_user_registration_path) %>"><%= link_to "Sign up", new_user_registration_path %></li>
<li class="<%= 'active' if current_page?(new_user_session_path) %>"><%= link_to "Sign in", new_user_session_path %></li>
<% end %>
</ul>
</div>
</nav>
<div class="container">
<% if notice %>
<div class="alert alert-info"><%= notice %></div>
<% end %>
<% if alert %>
<div class="alert alert-danger"><%= alert %></div>
<% end %>
</div>
app/views/tweets/index.html.erb
<ul class="list-group">
...
<li class="list-group-item">
...
<small class="text-muted">
$ rails c
User.first
User.create(email: 'other@example.com', password: '123456789', password_confirmation: '123456789')
u = User.find(2)
u.tweets.create(content: 'Tweet from console')
u.tweets.create(content: 'Anoher tweet from console')
app/controllers/home_controller.rb
def index
@users = User.all
end
app/views/home/index.html.erb
<ul>
<% @users.each do |user| %>
<li>
<%= user.email %>
</li>
<% end %>
</ul>
app/views/home/index.html.erb
<%= link_to user.email, tweets_path(user_id: user.id) %>
app/controllers/tweets_controller.rb
if params[:user_id]
user = User.find(params[:user_id])
else
user = current_user
end
@tweets = user.tweets.order(created_at: :desc)
app/controllers/tweets_controller.rb
if params[:user_id]
@user = User.find(params[:user_id])
else
@user = current_user
end
@tweets = @user.tweets.order(created_at: :desc)
app/views/tweets/index.html.erb
<h3><%= @user.email %> tweets</h3>