Skip to content

Instantly share code, notes, and snippets.

@mkwiatkowski
Last active January 3, 2016 08:49
Show Gist options
  • Save mkwiatkowski/8438690 to your computer and use it in GitHub Desktop.
Save mkwiatkowski/8438690 to your computer and use it in GitHub Desktop.
Bloc workshop cheatsheet

The code we're gonna use is at https://github.com/mkwiatkowski/rails4-with-devise-seed

Workshop cheatsheet

1. Creating a new project

$ git clone https://github.com/mkwiatkowski/rails4-with-devise-seed.git twitter
$ cd twitter
$ bundle
$ rake db:setup
$ rails s

2. Tweets controller

$ rails g controller tweets

app/controllers/tweets_controller.rb

class TweetsController < ApplicationController
  def new
  end
end

config/routes.rb

resources :tweets
$ rake routes

3. New tweet form

<%= 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

4. List of tweets

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)

5. Layout

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 %>

6. Bootstrap

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">

7. Rails console

$ 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')

8. List of users

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>

Users table

id email encrypted_password created_at updated_at
1 michal@trivas.pl QClbrYXDrCgDkBZvTaatu2Yb3W 2014-01-11 11:28:16.541116 2014-01-28 19:30:05.274902
2 prasid@bloc.io xHTJXzETQu.5FLxR.UyuwnVQ6q 2014-01-11 13:17:07.591725 2014-01-11 13:30:14.343957

Tweets table

id content user_id created_at updated_at
1 Hello world! 1 2014-01-15 14:29:25.284698 2014-01-15 14:29:25.284698
2 Programming Twitter 1 2014-01-15 15:44:01.681959 2014-01-15 15:44:01.681959
3 Twitter is great 2 2014-01-30 19:08:59.076502 2014-01-30 19:08:59.076502
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment