Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog creation and additions #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
10 changes: 9 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -197,6 +204,7 @@ GEM

PLATFORMS
ruby
x64-mingw32

DEPENDENCIES
bootsnap (>= 1.4.4)
Expand All @@ -217,7 +225,7 @@ DEPENDENCIES
webpacker (~> 5.0)

RUBY VERSION
ruby 2.7.2p137
ruby 2.7.3p183

BUNDLED WITH
2.1.4
82 changes: 67 additions & 15 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/comments.scss
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
@@ -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/
35 changes: 35 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/comments_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module CommentsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
3 changes: 3 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Comment < ApplicationRecord
belongs_to :post
end
20 changes: 20 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

<p><%= comment.body %></p>
<div>
<%= link_to "Edit", edit_post_comment_path(comment.post, comment), :class => 'btn-smstroke' %>
<%= link_to 'Delete', [comment.post, comment], method: :delete, :class => 'btn-smstroke' %>
</div>
<hr />
8 changes: 8 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= form_with model: [ post, post.comments.build ] do |form| %>
<p>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
14 changes: 14 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<h2>Edit Comment</h2>
<div>
<%= form_with model: @comment do |form| %>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
<p>Comment last updated: <%= @comment.updated_at.strftime("%B %d %Y, %l:%M%P") %></p>

</div>
6 changes: 6 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
</head>

<body>
<section class="header">
<h3 class="title">Career Plug</h3>
<%= link_to "+ New Post", new_post_path, :class => 'btn-bgstroke' %>
</section>
<section class="narrow_body">
<%= yield %>
</section>
</body>
</html>
17 changes: 17 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

<%= form_tag(posts_path, method: :get) do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag 'Search', name: nil %>
<% end %>

<% @posts.each do |post|%>
<%= content_tag :div, class: "blog_title" do -%>
<%= post.title %>
<% end -%>
<%= content_tag :div, class: "blog_body" do -%>
<%= post.body %>
<% end -%>
<div>
<%= link_to "comments (#{post.comments.count})", post %>
</div>
<% end %>
23 changes: 23 additions & 0 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h1>Create Post</h1>

<%= form_with model: @post do |form| %>
<div>
<%= form.label :title %><br>
<%= form.text_field :title %>
<% @post.errors.full_messages_for(:title).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.label :body %><br>
<%= form.text_area :body %><br>
<% @post.errors.full_messages_for(:body).each do |message| %>
<div><%= message %></div>
<% end %>
</div>

<div>
<%= form.submit %>
</div>
<% end %>
17 changes: 17 additions & 0 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1 class="blog_title"><%= @post.title %></h1>
<div>
<p><%= @post.created_at.strftime("%B %d %Y, %l:%M%P") %></p>
<p><%= @post.body %></p>
<%= link_to "All Blogs", posts_path, :class => 'btn-smstroke' %>
<%= link_to "Delete",
post_path(@post),
method: :delete,
data: { confirm: "Are you sure?"},
:class => 'btn-smstroke'
%>
</div>
<hr />
<h2>Comments</h2>

<%= render @post.comments %>
<%= render 'comments/form', post: @post %>
10 changes: 9 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
root "posts#index"

# basic CRUD endpoints and other features for POSTS, with Comments nested
resources :posts do
resources :comments
end

# including resources for comments for Update/Delete
resources :comments
end
10 changes: 10 additions & 0 deletions db/migrate/20210608015346_create_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePosts < ActiveRecord::Migration[6.1]
def change
create_table :posts do |t|
t.string :title
t.text :body

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20210608015443_create_comments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateComments < ActiveRecord::Migration[6.1]
def change
create_table :comments do |t|
t.text :body
t.references :post, null: false, foreign_key: true

t.timestamps
end
end
end
Loading