diff --git a/app/assets/javascripts/blogs.js b/app/assets/javascripts/blogs.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/blogs.js @@ -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. diff --git a/app/assets/stylesheets/blogs.scss b/app/assets/stylesheets/blogs.scss new file mode 100644 index 0000000..59c4c25 --- /dev/null +++ b/app/assets/stylesheets/blogs.scss @@ -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/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..6045188 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -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; +} diff --git a/app/controllers/blogs_controller.rb b/app/controllers/blogs_controller.rb new file mode 100644 index 0000000..a4019ef --- /dev/null +++ b/app/controllers/blogs_controller.rb @@ -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 diff --git a/app/helpers/blogs_helper.rb b/app/helpers/blogs_helper.rb new file mode 100644 index 0000000..cc0dbd2 --- /dev/null +++ b/app/helpers/blogs_helper.rb @@ -0,0 +1,2 @@ +module BlogsHelper +end diff --git a/app/models/blog.rb b/app/models/blog.rb new file mode 100644 index 0000000..07aa7df --- /dev/null +++ b/app/models/blog.rb @@ -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) +# diff --git a/app/models/user.rb b/app/models/user.rb index d2f75f3..57443da 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/blogs/_form.html.erb b/app/views/blogs/_form.html.erb new file mode 100644 index 0000000..652e704 --- /dev/null +++ b/app/views/blogs/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_with(model: blog, local: true) do |form| %> + <% if blog.errors.any? %> +
<%= notice %>
+ +| Name | +Alias | +User | ++ | ||
|---|---|---|---|---|---|
| <%= blog.name %> | +<%= blog.alias %> | +<%= blog.user %> | +<%= link_to 'Show', blog %> | +<%= link_to 'Edit', edit_blog_path(blog) %> | +<%= link_to 'Destroy', blog, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ Name: + <%= @blog.name %> +
+ ++ Alias: + <%= @blog.alias %> +
+ ++ User: + <%= @blog.user %> +
+ +<%= link_to 'Edit', edit_blog_path(@blog) %> | +<%= link_to 'Back', blogs_path %> diff --git a/config/routes.rb b/config/routes.rb index f946764..ff04d53 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20170824183642_create_blogs.rb b/db/migrate/20170824183642_create_blogs.rb new file mode 100644 index 0000000..13d26d3 --- /dev/null +++ b/db/migrate/20170824183642_create_blogs.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index d484d03..d17726f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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