Add inital base paste functionality
This commit is contained in:
parent
268db267ed
commit
f2dbc7f981
15 changed files with 127 additions and 7 deletions
|
@ -1,19 +1,39 @@
|
|||
class PastesController < ApplicationController
|
||||
def index
|
||||
@pastes = Paste.all
|
||||
end
|
||||
|
||||
def create
|
||||
def show
|
||||
@paste = Paste.find(params[:id])
|
||||
end
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def update
|
||||
@paste = Paste.new
|
||||
end
|
||||
|
||||
def edit
|
||||
end
|
||||
|
||||
def create
|
||||
@paste = Paste.new(paste_params)
|
||||
|
||||
if @paste.save
|
||||
flash[:success] = "Your paste has been added"
|
||||
redirect_to @paste
|
||||
else
|
||||
flash.now[:danger] = "There was an error submitting your form"
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
|
||||
private
|
||||
def paste_params
|
||||
params.require(:paste).permit(:content)
|
||||
end
|
||||
end
|
||||
|
|
3
app/models/paste.rb
Normal file
3
app/models/paste.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Paste < ActiveRecord::Base
|
||||
validates :content, presence: true
|
||||
end
|
3
app/views/layouts/_error.html.erb
Normal file
3
app/views/layouts/_error.html.erb
Normal file
|
@ -0,0 +1,3 @@
|
|||
<% flash.each do |key, value| %>
|
||||
<%= content_tag(:div, value, class: "alert alert-#{key}") %>
|
||||
<% end %>
|
|
@ -13,7 +13,7 @@
|
|||
<body>
|
||||
<nav class="navbar navbar-inverse" role="navigation">
|
||||
<div class="navbar-header">
|
||||
<a class="navbar-brand" href="#">Tack2Us</a>
|
||||
<a class="navbar-brand" href="/">Tack2Us</a>
|
||||
</div>
|
||||
<div class="collapse navbar-collapse">
|
||||
<ul class="nav navbar-nav navbar-right">
|
||||
|
@ -24,6 +24,7 @@
|
|||
</div>
|
||||
</nav>
|
||||
<div class="container">
|
||||
<%= render partial: 'layouts/error' %>
|
||||
<%= yield %>
|
||||
</div>
|
||||
</body>
|
||||
|
|
7
app/views/pastes/_form.html.erb
Normal file
7
app/views/pastes/_form.html.erb
Normal file
|
@ -0,0 +1,7 @@
|
|||
<%= form_for @paste do |f| %>
|
||||
<div class="form-group">
|
||||
<%= f.label :content %>
|
||||
<%= f.text_area :content, class: 'form-control' %>
|
||||
</div>
|
||||
<%= f.submit 'Paste it!', class: 'btn btn-primary' %>
|
||||
<% end %>
|
|
@ -1 +1,3 @@
|
|||
Edit
|
||||
|
||||
<% render '_form' %>
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
Hello, World!
|
||||
<% @pastes.each do |paste| %>
|
||||
<%= link_to paste.id, paste %><br />
|
||||
<% end %>
|
||||
|
|
|
@ -1 +1 @@
|
|||
New
|
||||
<%= render partial: 'form' %>
|
||||
|
|
1
app/views/pastes/show.html.erb
Normal file
1
app/views/pastes/show.html.erb
Normal file
|
@ -0,0 +1 @@
|
|||
<%= @paste.content %>
|
8
db/migrate/20140214151745_create_pastes.rb
Normal file
8
db/migrate/20140214151745_create_pastes.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
class CreatePastes < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :pastes do |t|
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
5
db/migrate/20140214152056_add_content_to_pastes.rb
Normal file
5
db/migrate/20140214152056_add_content_to_pastes.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddContentToPastes < ActiveRecord::Migration
|
||||
def change
|
||||
add_column :pastes, :content, :string
|
||||
end
|
||||
end
|
|
@ -0,0 +1,9 @@
|
|||
class ChangeContentTypeInPastes < ActiveRecord::Migration
|
||||
def self.up
|
||||
change_column :pastes, :content, :text
|
||||
end
|
||||
|
||||
def self.down
|
||||
change_column :pastes, :content, :string
|
||||
end
|
||||
end
|
22
db/schema.rb
Normal file
22
db/schema.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
# encoding: UTF-8
|
||||
# This file is auto-generated from the current state of the database. Instead
|
||||
# of editing this file, please use the migrations feature of Active Record to
|
||||
# incrementally modify your database, and then regenerate this schema definition.
|
||||
#
|
||||
# Note that this schema.rb definition is the authoritative source for your
|
||||
# database schema. If you need to create the application database on another
|
||||
# system, you should be using db:schema:load, not running all the migrations
|
||||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
||||
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20140214185753) do
|
||||
|
||||
create_table "pastes", force: true do |t|
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
t.text "content", limit: 255
|
||||
end
|
||||
|
||||
end
|
19
spec/features/creates_new_paste_spec.rb
Normal file
19
spec/features/creates_new_paste_spec.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
require 'spec_helper'
|
||||
|
||||
feature 'Creates new paste' do
|
||||
scenario 'with valid content' do
|
||||
visit new_paste_path
|
||||
fill_in 'Content', with: 'Test'
|
||||
click_button 'Paste it!'
|
||||
|
||||
expect(page).to have_content('Your paste has been added')
|
||||
end
|
||||
|
||||
scenario 'with blank content' do
|
||||
visit new_paste_path
|
||||
fill_in 'Content', with: ' '
|
||||
click_button 'Paste it!'
|
||||
|
||||
expect(page).to have_content('There was an error submitting your form')
|
||||
end
|
||||
end
|
18
spec/models/paste_spec.rb
Normal file
18
spec/models/paste_spec.rb
Normal file
|
@ -0,0 +1,18 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Paste do
|
||||
before do
|
||||
@paste = Paste.new(content: "Test")
|
||||
end
|
||||
|
||||
subject { @paste }
|
||||
|
||||
it { should respond_to(:content) }
|
||||
|
||||
it { should be_valid }
|
||||
|
||||
describe "when content is blank" do
|
||||
before { @paste.content = " " }
|
||||
it { should_not be_valid }
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue