diff app/controllers/tweets_controller.rb @ 55:8f68ca606099

Add twitter id to the url so it'll be consistent. Not sure if there will be use case of "following this id".
author nanaya <me@myconan.net>
date Fri, 03 Jul 2015 01:17:40 +0900
parents 8983c426e256
children 1161ad2a4390
line wrap: on
line diff
--- a/app/controllers/tweets_controller.rb	Sat Jun 27 14:47:28 2015 +0900
+++ b/app/controllers/tweets_controller.rb	Fri Jul 03 01:17:40 2015 +0900
@@ -1,4 +1,6 @@
 class TweetsController < ApplicationController
+  before_action :validate_id, :only => :show
+
   def index
   end
 
@@ -11,4 +13,23 @@
   rescue Twitter::Error::Unauthorized
     head :forbidden
   end
+
+  private
+
+  def validate_id
+    id = params[:id].split("/")[0]
+    int_id = id.to_i
+    if int_id.to_s == id
+      params[:id] = int_id
+    else
+      try_redirect
+    end
+  end
+
+  def try_redirect
+    user = Tweet.new(params[:id]).user
+    redirect_to tweet_path("#{user.id}/#{user.name}")
+  rescue Twitter::Error::NotFound
+    head :not_found
+  end
 end