Skip to content

Instantly share code, notes, and snippets.

@wtachau
Created May 20, 2015 21:37
Show Gist options
  • Select an option

  • Save wtachau/be1cf1b3621732ef50e4 to your computer and use it in GitHub Desktop.

Select an option

Save wtachau/be1cf1b3621732ef50e4 to your computer and use it in GitHub Desktop.
Return JSON instead of server-side rendering
class CommentsController < ApplicationController
include CommentsHelper
def create
@comment = (current_user.comments.create comments_params).decorate
tagged_users = get_tagged_users(@comment)
# render @comment
# @current_post = Post.find(id: comments_params[:post_id])
@current_post = Post.first
render json: @current_post.comments
Thread.new do
NotificationMailer.users_tagged(current_user, tagged_users, @comment).deliver_now
NotificationMailer.commented_post(@comment).deliver_now
end
end
private
def comments_params
params.require(:comment).permit(:post_id, :text, :image)
end
# Given the comment text, return list of user ids tagged
def get_tagged_users(comment)
ids = []
get_user_ids = lambda { |match| ids.push(userFromTag(match).email) }
get_users_from(comment.text, get_user_ids)
ids
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment