Created
April 9, 2016 20:56
-
-
Save phensalves/67603a264678bf8c51c4c8c5c96864f7 to your computer and use it in GitHub Desktop.
Forum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%table | |
%thead | |
%tr | |
%th Number | |
%th Created At | |
%th Text | |
%th | |
%th | |
%th | |
%tbody | |
- @comments.each do |c| | |
%tr | |
%td | |
= link_to c.number, comment_path(@subject,c) | |
%td | |
= link_to c.created_at, comment_path(@subject,c) | |
%td | |
= c.text | |
%td | |
.actions | |
= link_to "Show", show_comment_path(@subject, c) | |
= link_to "Reply", new_comment_path(:parent_id => c) | |
= link_to 'Destroy', c, :method => :delete, :data => { :confirm => 'Are you sure?' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for [@subject, @subject.comments.new] do |f| | |
- if @comment.errors.any? | |
#error_explanation | |
%h2= "#{pluralize(@comment.errors.count, "error")} prohibited this comment from being saved:" | |
%ul | |
- @comment.errors.full_messages.each do |msg| | |
%li= msg | |
.field | |
= f.label :text, 'New Message' | |
= f.text_area :text, :rows => 8 | |
.actions | |
= f.submit 'Reply' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'autoinc' | |
class Comment | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Mongoid::Autoinc | |
field :number, type:Integer | |
increments :number | |
field :text, type: String | |
field :_id, type: String | |
belongs_to :subject | |
belongs_to :comment | |
has_many :comments, class_name: 'Subject' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CommentsController < ApplicationController | |
before_action :set_comment, only: [:show, :edit, :update, :destroy] | |
before_action :get_subject | |
def index | |
@comments = @subject.comments.all | |
end | |
def show | |
end | |
def new | |
@comment = Comment.new | |
@comment.subject = Subject.find(params[:subject_id]) | |
@comment.comment = Comment.find(params[:comment_id]) if params[:comment_id] | |
@comment | |
end | |
def edit | |
end | |
def create | |
@comment = Comment.new(comment_params) | |
respond_to do |format| | |
if @comment.save | |
format.html { redirect_to subjects_path(@subject, @comment), notice: 'Comment was successfully created.' } | |
format.json { render :show, status: :created, location: @comment } | |
else | |
format.html { render :new } | |
format.json { render json: @comment.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def update | |
respond_to do |format| | |
if @comment.update(comment_params) | |
format.html { redirect_to subjects_path(@subject, @comment), notice: 'Comment was successfully updated.' } | |
format.json { render :show, status: :ok, location: @comment } | |
else | |
format.html { render :edit } | |
format.json { render json: @comment.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@comment.destroy | |
respond_to do |format| | |
format.html { redirect_to comments_url, notice: 'Comment was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def set_comment | |
@comment = @subject.comments.find(params[:id]) | |
end | |
def comment_params | |
params.require(:comment).permit(:text, :subject_id, :comment_id) | |
end | |
def get_subject | |
@subject = Subject.find(params[:subject_id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Editing comment | |
= render 'form' | |
= link_to 'Show', show_comment_path(@subject, @comment) | |
\| | |
= link_to 'Back', comments_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= form_for @subject do |f| | |
- if @subject.errors.any? | |
#error_explanation | |
%h2= "#{pluralize(@subject.errors.count, "error")} prohibited this subject from being saved:" | |
%ul | |
- @subject.errors.full_messages.each do |msg| | |
%li= msg | |
= f.label "Name", :s_name | |
= f.text_field :s_name | |
= f.label "Author", :s_author | |
= f.text_field :s_author | |
= f.label "Content", :content | |
= f.text_field :content | |
.actions | |
= f.submit 'Save' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Listing comments | |
= render 'comment' | |
%br | |
= link_to 'Back', subjects_path | |
\| | |
= link_to 'Write a comment', new_subject_comment_path(@subject) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Listing subjects | |
%table | |
%thead | |
%tr | |
%th | |
Number | |
%th | |
Created At | |
%th | |
Name | |
%th | |
%tbody | |
- @subjects.each do |s| | |
%tr | |
%td | |
= link_to s.number, s | |
%td | |
= link_to s.created_at, s | |
%td | |
%b= s.s_name | |
/ %td= link_to 'Show', s | |
/ %td= link_to 'Edit', edit_subject_path(s) | |
%td | |
= link_to 'See comments', subject_comments_path(s) | |
= link_to 'Destroy', s, :method => :delete, :data => { :confirm => 'Are you sure?' } | |
%br | |
= link_to 'New Subject', new_subject_path | |
%br | |
%br | |
= will_paginate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 Reply | |
= render 'form' | |
= link_to 'Write a comment', new_subject_comment_path(@subject) | |
\| | |
= link_to 'Back', subject_comments_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1 New subject | |
= render 'form' | |
= link_to 'Back', subjects_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%p | |
%b Text: | |
= @comment.text | |
%p | |
%b Subject: | |
= @comment.subject_id | |
%p | |
%b Comment: | |
= @comment.comment_id | |
= link_to 'Edit', edit_comment_path(@subject, @comment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'autoinc' | |
class Subject | |
include Mongoid::Document | |
include Mongoid::Timestamps | |
include Mongoid::Autoinc | |
field :number, type: Integer | |
increments :number | |
field :s_name, type: String | |
field :s_author, type: String | |
field :content, type: String | |
field :_id, type: String, default: -> {s_name.to_s.parameterize} | |
has_many :comments | |
default_scope -> { order(created_at: :desc) } | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubjectsController < ApplicationController | |
before_action :set_subject, only: [:show, :edit, :update, :destroy] | |
def index | |
@subjects = Subject.paginate(page: params[:page], per_page: 2) | |
end | |
def show | |
end | |
def new | |
@subject = Subject.new | |
end | |
def edit | |
end | |
def create | |
@subject = Subject.new(subject_params) | |
respond_to do |format| | |
if @subject.save | |
format.html { redirect_to @subject, notice: 'Subject was successfully created.' } | |
format.json { render :show, status: :created, location: @subject } | |
else | |
format.html { render :new } | |
format.json { render json: @subject.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def update | |
respond_to do |format| | |
if @subject.update(subject_params) | |
format.html { redirect_to @subject, notice: 'Subject was successfully updated.' } | |
format.json { render :show, status: :ok, location: @subject } | |
else | |
format.html { render :edit } | |
format.json { render json: @subject.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
def destroy | |
@subject.destroy | |
respond_to do |format| | |
format.html { redirect_to subjects_url, notice: 'Subject was successfully destroyed.' } | |
format.json { head :no_content } | |
end | |
end | |
private | |
def set_subject | |
@subject = Subject.find(params[:id]) | |
end | |
def subject_params | |
params.require(:subject).permit(:_id, :s_name, :s_author, :content) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment