diff --git a/Gemfile b/Gemfile index 90fa4e8..331d303 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.7.2' +ruby '2.7.3' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.3', '>= 6.1.3.2' diff --git a/Gemfile.lock b/Gemfile.lock index bddcebe..11ae1a3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -80,6 +80,7 @@ GEM crass (1.0.6) erubi (1.10.0) ffi (1.15.1) + ffi (1.15.1-x64-mingw32) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.8.10) @@ -104,6 +105,8 @@ GEM nokogiri (1.11.6) mini_portile2 (~> 2.5.0) racc (~> 1.4) + nokogiri (1.11.6-x64-mingw32) + racc (~> 1.4) public_suffix (4.0.6) puma (5.3.2) nio4r (~> 2.0) @@ -151,6 +154,8 @@ GEM sassc-rails (~> 2.1, >= 2.1.1) sassc (2.4.0) ffi (~> 1.9) + sassc (2.4.0-x64-mingw32) + ffi (~> 1.9) sassc-rails (2.1.2) railties (>= 4.0.0) sassc (>= 2.0) @@ -174,6 +179,8 @@ GEM tilt (2.0.10) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + tzinfo-data (1.2021.1) + tzinfo (>= 1.0.0) web-console (4.1.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -197,6 +204,7 @@ GEM PLATFORMS ruby + x64-mingw32 DEPENDENCIES bootsnap (>= 1.4.4) @@ -217,7 +225,7 @@ DEPENDENCIES webpacker (~> 5.0) RUBY VERSION - ruby 2.7.2p137 + ruby 2.7.3p183 BUNDLED WITH 2.1.4 diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d05ea0f..ee91c08 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,15 +1,67 @@ -/* - * This is a manifest file that'll be compiled into application.css, which will include all the files - * listed below. - * - * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's - * vendor/assets/stylesheets directory can be referenced here using a relative path. - * - * You're free to add application-wide styles to this file and they'll appear at the bottom of the - * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS - * files in this directory. Styles in this file should be added after the last require_* statement. - * It is generally better to create a new file per style scope. - * - *= require_tree . - *= require_self - */ +.header { + width: 100%; + height: 30px; + background: #7e7e7e; + color: white; + float: left; + padding-top: 5px; + padding-bottom: 5px; +} + +.btn-bgstroke { + font-size: 15px; + float: right; + border: 1px solid white; + padding: 5px 10px; + border-radius: 10px; + cursor: pointer; + font-weight: 300; + background-color: lightgray; + text-decoration: none; + color: black; + margin-right: 5px; +} + +.btn-bgstroke:hover { + background-color: white; + color: #7e7e7e; +} + +.btn-smstroke { + font-size: 15px; + border: 1px solid white; + padding: 5px 10px; + border-radius: 10px; + cursor: pointer; + font-weight: 300; + background-color: lightgray; + text-decoration: none; + color: black; + margin-right: 5px; +} + +.btn-smstroke:hover { + background-color: white; + color: #7e7e7e; +} + +.title { + display: inline; + margin-left: 10px; +} + +.narrow_body { + margin-left: auto; + margin-right: auto; + padding-top: 60px; + width: 25em; +} + +.blog_title { + font-size: 40px; +} + +.blog_body { + white-space: nowrap; + overflow: hidden; +} diff --git a/app/assets/stylesheets/comments.scss b/app/assets/stylesheets/comments.scss new file mode 100644 index 0000000..69088e8 --- /dev/null +++ b/app/assets/stylesheets/comments.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Comments controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/posts.scss b/app/assets/stylesheets/posts.scss new file mode 100644 index 0000000..6907cec --- /dev/null +++ b/app/assets/stylesheets/posts.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Posts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 0000000..595b520 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,35 @@ +class CommentsController < ApplicationController + def show + @post = Post.find(params[:post_id]) + @comment = @post.comments.find(params[:id]) + end + + def create + @post = Post.find(params[:post_id]) + @comment = @post.comments.create(comment_params) + redirect_to post_path(@post) + end + + def edit + @comment = Comment.find(params[:id]) + end + + def update + @comment = Comment.find(params[:id]) + @comment.update(comment_params) + @post = @comment.post + redirect_to post_path(@post) + end + + def destroy + @comment = Comment.find(params[:id]) + @post = @comment.post + @comment.destroy + redirect_to post_path(@post) + end + + private + def comment_params + params.require(:comment).permit(:user, :body) + end +end diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb new file mode 100644 index 0000000..0a923d8 --- /dev/null +++ b/app/controllers/posts_controller.rb @@ -0,0 +1,36 @@ +class PostsController < ApplicationController + def index + @posts = Post.search(params[:search]) + end + + def show + @post = Post.find(params[:id]) + end + + def new + @post = Post.new + end + + def create + @post = Post.new(post_params) + + if @post.save + redirect_to @post + else + render :new + end + end + + def destroy + @post = Post.find(params[:id]) + @post.destroy + + redirect_to root_path + end + + # to prohibit extra form fields being entered and overwriting of private data, setting explicit params -> + private + def post_params + params.require(:post).permit(:title, :body, :id, :search) + end +end diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 0000000..0ec9ca5 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/helpers/posts_helper.rb b/app/helpers/posts_helper.rb new file mode 100644 index 0000000..a7b8cec --- /dev/null +++ b/app/helpers/posts_helper.rb @@ -0,0 +1,2 @@ +module PostsHelper +end diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 0000000..8b86c56 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,3 @@ +class Comment < ApplicationRecord + belongs_to :post +end diff --git a/app/models/post.rb b/app/models/post.rb new file mode 100644 index 0000000..3d2768d --- /dev/null +++ b/app/models/post.rb @@ -0,0 +1,20 @@ +class Post < ApplicationRecord + has_many :comments, dependent: :destroy + + def self.search(search) + if search + post = Post.where(title: search).or(Post.where(body: search)) + + if post + self.where(id: post) + else + Post.all + end + else + Post.all + end + end + + validates :title, presence: true + validates :body, presence: true, length: { minimum: 10 } +end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb new file mode 100644 index 0000000..f2283b6 --- /dev/null +++ b/app/views/comments/_comment.html.erb @@ -0,0 +1,7 @@ + +
<%= comment.body %>
++ <%= form.text_area :body %> +
++ <%= form.submit %> +
+<% end %> \ No newline at end of file diff --git a/app/views/comments/edit.html.erb b/app/views/comments/edit.html.erb new file mode 100644 index 0000000..50e7270 --- /dev/null +++ b/app/views/comments/edit.html.erb @@ -0,0 +1,14 @@ +
+ <%= form.label :body %>
+ <%= form.text_area :body %>
+
+ <%= form.submit %> +
+ <% end %> +Comment last updated: <%= @comment.updated_at.strftime("%B %d %Y, %l:%M%P") %>
+ +