Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/assets/javascripts/blogs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/blogs.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Blogs controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
59 changes: 59 additions & 0 deletions app/controllers/blogs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class BlogsController < ApplicationController
before_action :set_blog, only: [:show, :edit, :update, :destroy]

# GET /blogs
def index
@blogs = Blog.all
end

# GET /blogs/1
def show

end

# GET /blogs/new
def new
@blog = Blog.new
end

# GET /blogs/1/edit
def edit
end

# POST /blogs
def create
@blog = Blog.new(blog_params)

if @blog.save
redirect_to @blog, notice: 'Blog was successfully created.'
else
render :new
end
end

# PATCH/PUT /blogs/1
def update
if @blog.update(blog_params)
redirect_to @blog, notice: 'Blog was successfully updated.'
else
render :edit
end
end

# DELETE /blogs/1
def destroy
@blog.destroy
redirect_to blogs_url, notice: 'Blog was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_blog
@blog = Blog.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def blog_params
params.require(:blog).permit(:name, :alias, :user_id)
end
end
2 changes: 2 additions & 0 deletions app/helpers/blogs_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module BlogsHelper
end
23 changes: 23 additions & 0 deletions app/models/blog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Blog < ApplicationRecord
belongs_to :user
end

# == Schema Information
#
# Table name: blogs
#
# alias :string(255)
# created_at :datetime not null
# id :integer not null, primary key
# name :string(255)
# updated_at :datetime not null
# user_id :integer
#
# Indexes
#
# index_blogs_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (user_id => users.id)
#
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, :email, presence: true
has_many :blogs

end



# == Schema Information
#
# Table name: users
Expand Down
32 changes: 32 additions & 0 deletions app/views/blogs/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%= form_with(model: blog, local: true) do |form| %>
<% if blog.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:</h2>

<ul>
<% blog.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name, id: :blog_name %>
</div>

<div class="field">
<%= form.label :alias %>
<%= form.text_field :alias, id: :blog_alias %>
</div>

<div class="field">
<%= form.label :user_id %>
<%= form.text_field :user_id, id: :blog_user_id %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/blogs/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Blog</h1>

<%= render 'form', blog: @blog %>

<%= link_to 'Show', @blog %> |
<%= link_to 'Back', blogs_path %>
31 changes: 31 additions & 0 deletions app/views/blogs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<p id="notice"><%= notice %></p>

<h1>Blogs</h1>

<table>
<thead>
<tr>
<th>Name</th>
<th>Alias</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @blogs.each do |blog| %>
<tr>
<td><%= blog.name %></td>
<td><%= blog.alias %></td>
<td><%= blog.user %></td>
<td><%= link_to 'Show', blog %></td>
<td><%= link_to 'Edit', edit_blog_path(blog) %></td>
<td><%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Blog', new_blog_path %>
5 changes: 5 additions & 0 deletions app/views/blogs/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Blog</h1>

<%= render 'form', blog: @blog %>

<%= link_to 'Back', blogs_path %>
19 changes: 19 additions & 0 deletions app/views/blogs/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Name:</strong>
<%= @blog.name %>
</p>

<p>
<strong>Alias:</strong>
<%= @blog.alias %>
</p>

<p>
<strong>User:</strong>
<%= @blog.user %>
</p>

<%= link_to 'Edit', edit_blog_path(@blog) %> |
<%= link_to 'Back', blogs_path %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

Rails.application.routes.draw do
resources :blogs
devise_for :users
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
11 changes: 11 additions & 0 deletions db/migrate/20170824183642_create_blogs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateBlogs < ActiveRecord::Migration[5.1]
def change
create_table :blogs do |t|
t.string :name
t.string :alias
t.references :user, foreign_key: true

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170812172006) do
ActiveRecord::Schema.define(version: 20170824183642) do

create_table "blogs", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
t.string "alias"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_blogs_on_user_id"
end

create_table "users", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "name"
Expand All @@ -30,4 +39,5 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "blogs", "users"
end