Skip to content

Instantly share code, notes, and snippets.

@mykecameron
Last active December 21, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mykecameron/6311966 to your computer and use it in GitHub Desktop.
Save mykecameron/6311966 to your computer and use it in GitHub Desktop.
# config/routes.rb
Threads::Application.routes.draw do
#...existing stuff
resources :topics, only: [:index, :create, :show] do
resources :posts, only: [:index, :create, :show]
end
resources :settings, only: [:index]
match '/topics/:topic_id/viewings' => 'viewings#create', :via => :post
end
# app/controllers/viewings_controller.rb
class ViewingsController < ApplicationController
def create
topic = Topic.find(params[:topic_id])
topic.view_count += 1
if topic.save
render text: '', status: 200
else
# handle errors
end
# in the future you could set other data you need here, eg: a timestamp on current_user for
# purposes of figuring out how many posts were added since you last looked at the topic
end
end
# app/assets/javascripts/router.js.coffee
class Threads.Router extends Backbone.Router
# existing stuff..
show: (topicId) ->
@_topics.fetch().done =>
topic = @_topics.get topicId
topic.posts().fetch
success: ->
Threads.app.renderContent new Threads.Views.Show topic: topic
$.post "/topics/#{topicId}/viewings"
#...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment