Last active
December 21, 2015 13:19
-
-
Save mykecameron/6311966 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
# 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