Created
August 6, 2016 19:15
-
-
Save gabrielpedepera/22cd2bc304bc16df895ff804c0bd58cd to your computer and use it in GitHub Desktop.
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
# app/views/posts/show.html.erb | |
<br><br> | |
<h2>Comments</h2> | |
<div id="comments"> | |
<%# render partial: 'comments/comment', collection: @posts.comments %> | |
<%= render @post.comments %> | |
</div> | |
<%= render 'comments/new', post: @post %> | |
# app/views/comments/_comment.html.erb | |
<p><%= comment.body %> -- <%= comment.created_at.to_s(:long) %></p> | |
# app/views/comments/_new.html.erb | |
<%= form_for([@post, Comment.new], remote: true) do |form| %> | |
Your Comment:<br> | |
<%= form.text_area :body, size: '50x20' %><br> | |
<%= form.submit %> | |
<% end %> | |
# app/controllers/comments_controller.rb | |
class CommentsController < ApplicationController | |
before_action :set_post | |
def create | |
@post.comments.create! comments_params | |
redirect_to @post | |
end | |
private | |
def set_post | |
@post = Post.find(params[:post_id]) | |
end | |
def comments_params | |
params.required(:comment).permit(:body) | |
end | |
end | |
# mailer | |
rails g mailer comments submitted | |
# app/mailers/comments_mailer.rb | |
class CommentsMailer < ApplicationMailer | |
# Subject can be set in your I18n file at config/locales/en.yml | |
# with the following lookup: | |
# | |
# en.comments_mailer.submitted.subject | |
# | |
def submitted(comment) | |
@comment = comment | |
mail to: "blog-owner@example.org", subject: 'New Comment' | |
end | |
end | |
# app/views/comments_mailers/submitted.html.erb | |
<h1>You got a new comment on <%= @comment.post.title %></h1> | |
<%= render @comment %> | |
# app/views/comments_mailers/submitted.text.erb | |
You got a new comment on <%= @comment.post.title %>: <%= @comment.body %> | |
# test/mailers/previews/comments_mailer_preview.rb | |
# Preview all emails at http://localhost:3000/rails/mailers/comments_mailer | |
class CommentsMailerPreview < ActionMailer::Preview | |
# Preview this email at http://localhost:3000/rails/mailers/comments_mailer/submitted | |
def submitted | |
CommentsMailer.submitted Comment.first | |
end | |
end | |
https://pos-univem-gabrielpedepera.c9users.io/rails/mailers/comments_mailer/submitted.html | |
# app/controllers/comments_controller.rb | |
comment = @post.comments.create! comments_params | |
CommentsMailer.submitted(comment).deliver_later | |
# Channel | |
rails g channel comments | |
# app/channels/comments_channel.rb (server_side) | |
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading. | |
class CommentsChannel < ApplicationCable::Channel | |
def self.broadcast(comment) | |
broadcast_to comment.post, comment: | |
CommentsController.render(partial: 'comments/comment', locals: { comment: comment }) | |
end | |
def subscribed | |
stream_for Post.last | |
end | |
def unsubscribed | |
# Any cleanup needed when channel is unsubscribed | |
end | |
end | |
# app/assets/javascripts/channels/comments.coffee | |
App.comments = App.cable.subscriptions.create "CommentsChannel", | |
connected: -> | |
# Called when the subscription is ready for use on the server | |
disconnected: -> | |
# Called when the subscription has been terminated by the server | |
received: (data) -> | |
$('#comments').append data.comment | |
# app/controllers/comments_controller.rb | |
CommentsChannel.broadcast(comment) | |
# config.ru | |
ActionCable.server.config.disable_request_forgery_protection = true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment