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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

#I put this piece here
gem 'therubyracer'

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
libv8 (3.16.14.19)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -110,6 +111,7 @@ GEM
rb-fsevent (0.10.4)
rb-inotify (0.10.1)
ffi (~> 1.0)
ref (2.0.0)
sass (3.7.4)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
Expand All @@ -133,6 +135,9 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
therubyracer (0.12.3)
libv8 (~> 3.16.14.15)
ref
thor (1.0.1)
thread_safe (0.3.6)
tilt (2.0.10)
Expand Down Expand Up @@ -167,6 +172,7 @@ DEPENDENCIES
spring
spring-watcher-listen (~> 2.0.0)
sqlite3 (~> 1.3.6)
therubyracer
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/comments.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/pages.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/javascripts/posts.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
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: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/pages.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Pages controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://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: http://sass-lang.com/
89 changes: 89 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
margin: 33px;
}

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

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-bottom: 7px;
padding-left: 5px;
padding-right: 5px;
}

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;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;

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

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

label {
display: block;
}
38 changes: 38 additions & 0 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class CommentsController < ApplicationController
before_action :set_post
before_action :set_comment, only: [:edit, :update, :destroy]
# No `new` action because form is provided by PostsController#show

def create
# Create associated model, just like we did in the console before
@comment = @post.comments.create(comment_params)
# We want to show the comment in the context of the Post
redirect_to @post
end

def edit
end

def update
@comment = @comment.update(comment_params)
redirect_to @post
end

def destroy
@comment.destroy
redirect_to @post
end

private
def comment_params
params.require(:comment).permit(:name, :content)
end

def set_post
@post = Post.find(params[:post_id])
end

def set_comment
@comment = Comment.find(params[:id])
end
end
62 changes: 62 additions & 0 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class PagesController < ApplicationController
before_action :set_page, only: [:show, :edit, :update, :destroy]
def index
@pages = Page.all
end

def show
# @page = Page.find(params[:id]) # page in singular form, because this is to show one signle page
# render text: @page.title #Rails only renders one thing per controller action
# set_page
# this was called by before_action method, so we don't need to call this method explicitly anymore.
# rescue ActiveRecord::RecordNotFound
# flash[:notice] = "We couldn't find that page"
# redirect_to action: :index
end

def new
@page = Page.new
#stores a new Page object with blank attributes in the @page instance variable,
#for rendering in the view's form.
end

def create
# page_params = params.require(:page).permit(:body, :title, :slug)
# this line of code has been replaced by page_params method
@page = Page.new(page_params)
@page.save
redirect_to @page
end

def edit
# @page = Page.find(params[:id]) #is replace by
# set_page # is replace by before_action method
end

def update
# @page = Page.find(params[:id])
# set_page
# page_params = params.require(:page).permit(:body, :title, :slug)
@page.update(page_params)
redirect_to @page
end

def destroy
# @page = Page.find(params[:id])
# set_page
@page.destroy
redirect_to pages_path
end

private
def page_params
params.require(:page).permit(:body, :title, :slug)
end

def set_page
@page = Page.find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:notice] = "We couldn't find that page"
redirect_to action: :index
end
end
77 changes: 77 additions & 0 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]

# GET /posts
# GET /posts.json
def index
@posts = Post.all
#we could add an explicit call to the rendor method and get the same result.
#we could render the posts/index template using a layout of application, same result
# render template: "posts/index.html.erb", layout: "application"
end

# GET /posts/1
# GET /posts/1.json
def show
end

# GET /posts/new
def new
@post = Post.new
end

# GET /posts/1/edit
def edit
end

# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)

respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end

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

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:title, :body)
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/pages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PagesHelper
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
2 changes: 2 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Page < ApplicationRecord
end
3 changes: 3 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Post < ApplicationRecord #this line means Post is the subclass of ApplicationRecord class
has_many :comments
end
12 changes: 12 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<strong><%= comment.name %> says:</strong>
<%= comment.content %>

<div class="comment-admin">
<%= link_to "Edit", edit_post_comment_path(@post, comment) %>
<%= link_to "Delete", [@post, comment], method: :delete %>
</div>

</div>


15 changes: 15 additions & 0 deletions app/views/comments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<%= form_for [@post, comment] do |f| %>
<div class="field">
<%= f.label :name %>
<%= f.text_field :name %>
</div>

<div class="field">
<%= f.label :content, "Comment" %>
<%= f.text_area :content %>
</div>

<div class="actions">
<%= f.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/comments/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1> Edit Comment</h1>
<%= render partial: "form", locals: {comment: @comment} %>
Loading