Skip to content

Instantly share code, notes, and snippets.

@shanebarringer
Created August 18, 2016 14:51
Show Gist options
  • Save shanebarringer/a2ad98ffd88e57a98c0fd3b5d535066a to your computer and use it in GitHub Desktop.
Save shanebarringer/a2ad98ffd88e57a98c0fd3b5d535066a to your computer and use it in GitHub Desktop.
Build Twitter - part 2
<p id="notice"><%= notice %></p>
<h1>Whatever, this is just a header</h1>
<table>
<thead>
<tr>
<th>Message</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tweets.each do |tweet| %>
<tr>
<td><%= tweet.message %></td>
<td><%= tweet.user.username %></td>
<td><%= link_to 'Show', tweet_path(tweet.id) %></td>
<td><%= link_to 'Edit', edit_tweet_path(tweet.id) %></td>
<td><%= link_to 'Destroy', tweet, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Tweet', new_tweet_path %>
<h1>New Tweet</h1>
<%= form_for(@tweet) do |f| %>
<% if @tweet.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@tweet.errors.count, "error") %> prohibited this tweet from being saved:</h2>
<ul>
<% @tweet.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :message %><br>
<%= f.text_field :message %>
</div>
<div class="field">
<%= f.label :user_id %><br>
<%= f.text_field :user_id, value: current_user.id %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', tweets_path %>
Rails.application.routes.draw do
resources :tweets
root 'tweets#index'
devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
<p id="notice"><%= notice %></p>
<p>
<strong>Message:</strong>
<%= @tweet.message %>
</p>
<p>
<strong>User:</strong>
<%= @tweet.user.username %>
</p>
<%= link_to 'Edit', edit_tweet_path(@tweet) %> |
<%= link_to 'Back', tweets_path %>
class TweetsController < ApplicationController
before_action :set_tweet, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
def create
@tweet = Tweet.new(tweet_params)
respond_to do |format|
if @tweet.save
format.html { redirect_to @tweet, notice: "tweet was succesfully created"}
else
format.html { render :new }
end
end
end
def index
@tweets = Tweet.all
end
def new
@tweet = Tweet.new
end
def edit
end
def show
end
def update
end
def destroy
end
private
def set_tweet
@tweet = Tweet.find(params[:id])
end
def tweet_params
params.require(:tweet).permit(:message, :user_id)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment