From 23a6bbb348e06520c9f00e3be1bc7056661e9284 Mon Sep 17 00:00:00 2001 From: yuraskaz Date: Sun, 27 Aug 2017 00:12:45 +0300 Subject: [PATCH] #4-add-model-blog-crud --- app/assets/javascripts/blogs.js | 2 + app/assets/stylesheets/blogs.scss | 3 + app/assets/stylesheets/scaffolds.scss | 84 +++++++++++++++++++++++ app/controllers/blogs_controller.rb | 59 ++++++++++++++++ app/helpers/blogs_helper.rb | 2 + app/models/blog.rb | 23 +++++++ app/models/user.rb | 3 + app/views/blogs/_form.html.erb | 32 +++++++++ app/views/blogs/edit.html.erb | 6 ++ app/views/blogs/index.html.erb | 31 +++++++++ app/views/blogs/new.html.erb | 5 ++ app/views/blogs/show.html.erb | 19 +++++ config/routes.rb | 1 + db/migrate/20170824183642_create_blogs.rb | 11 +++ db/schema.rb | 12 +++- 15 files changed, 292 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/blogs.js create mode 100644 app/assets/stylesheets/blogs.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/blogs_controller.rb create mode 100644 app/helpers/blogs_helper.rb create mode 100644 app/models/blog.rb create mode 100644 app/views/blogs/_form.html.erb create mode 100644 app/views/blogs/edit.html.erb create mode 100644 app/views/blogs/index.html.erb create mode 100644 app/views/blogs/new.html.erb create mode 100644 app/views/blogs/show.html.erb create mode 100644 db/migrate/20170824183642_create_blogs.rb 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? %> +
+

<%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:

+ + +
+ <% end %> + +
+ <%= form.label :name %> + <%= form.text_field :name, id: :blog_name %> +
+ +
+ <%= form.label :alias %> + <%= form.text_field :alias, id: :blog_alias %> +
+ +
+ <%= form.label :user_id %> + <%= form.text_field :user_id, id: :blog_user_id %> +
+ +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/blogs/edit.html.erb b/app/views/blogs/edit.html.erb new file mode 100644 index 0000000..dd57c11 --- /dev/null +++ b/app/views/blogs/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Blog

+ +<%= render 'form', blog: @blog %> + +<%= link_to 'Show', @blog %> | +<%= link_to 'Back', blogs_path %> diff --git a/app/views/blogs/index.html.erb b/app/views/blogs/index.html.erb new file mode 100644 index 0000000..7ba0bbe --- /dev/null +++ b/app/views/blogs/index.html.erb @@ -0,0 +1,31 @@ +

<%= notice %>

+ +

Blogs

+ + + + + + + + + + + + + <% @blogs.each do |blog| %> + + + + + + + + + <% end %> + +
NameAliasUser
<%= 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?' } %>
+ +
+ +<%= link_to 'New Blog', new_blog_path %> diff --git a/app/views/blogs/new.html.erb b/app/views/blogs/new.html.erb new file mode 100644 index 0000000..0da5c24 --- /dev/null +++ b/app/views/blogs/new.html.erb @@ -0,0 +1,5 @@ +

New Blog

+ +<%= render 'form', blog: @blog %> + +<%= link_to 'Back', blogs_path %> diff --git a/app/views/blogs/show.html.erb b/app/views/blogs/show.html.erb new file mode 100644 index 0000000..6a7a33a --- /dev/null +++ b/app/views/blogs/show.html.erb @@ -0,0 +1,19 @@ +

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