Skip to content

Instantly share code, notes, and snippets.

@kibaekr
Created July 9, 2012 12:13
Show Gist options
  • Save kibaekr/3076131 to your computer and use it in GitHub Desktop.
Save kibaekr/3076131 to your computer and use it in GitHub Desktop.
session variable for request.referer doesn't actually store the url value
class ApplicationController < ActionController::Base
protect_from_forgery
#session['user_return_to'] = request.url
def after_sign_in_path_for(resource)
sign_in_url = "http://localhost:3000/users/sign_in" || "http://onvard.com/users/sign_in" ||
"http://www.onvard.com/users/sign_in" #url_for(:action => 'new', :controller => 'sessions', :only_path => false, :protocol => 'http')
if (request.referer == sign_in_url)
session[:current_path] || env['omniauth.origin'] || request.env['omniauth.origin'] || stored_location_for(resource) || root_path
else
request.referer
end
end
private
def store_location
session[:page_a] = request.referer
session[:current_path] = "http://localhost:3000" + request.fullpath
puts 'aaaaaaaaa below is page a, which should be missions_path'
puts session[:page_a]
#looking at log, this displays the missions_path at first, but after the user is logged in, it changes to 'user/sign_in'. I still can't get the session[:page_a] to store an absolute value.
puts 'aaaaaaaaaa below should be votes'
puts session[:current_path]
end
end
class MissionsController < ApplicationController
before_filter :store_location
before_filter :authenticate_user!, :except => [:show, :index]
def vote_for_mission
@mission = Mission.find(params[:id])
@originalpage = session[:page_a]
puts 'vvvvvvv22 below is original page that should be missions_path, not user sign in'
puts @originalpage
#looking at the logs, @originalpage stores user/sign_in
puts session[:page_a]
if @mission.voted_by?(current_user)
redirect_to @originalpage, alert: 'You already voted on this mission.'
else
@mission.increment!(:karma)
@mission.active = true
@mission.real_author.increment!(:userpoints) unless @mission.real_author.blank?
current_user.vote_for(@mission)
redirect_to @originalpage, notice: 'Your vote was successfully recorded.'
end
end
end
When I change the "redirect_to @originalpage" to a hardcoded value such as "redirect_to missions_path", it works perfectly fine. That is, it logs the user in, and then performs the vote action, and then redirects back to the missions_path, which is the original page where everything started. However, when I try to store this original page under session[:page_a]&@originalpage, it changes to "user/sign_in"
Started POST "/vote_for_mission_mission?id=1" for 127.0.0.1 at 2012-07-09 21:04:22 +0900
aaaaaaaaa below is page a, which should be missions_path
http://localhost:3000/missions
aaaaaaaaaa below should be votes
http://localhost:3000/vote_for_mission_mission?id=1
Processing by MissionsController#vote_for_mission as HTML
Parameters: {"authenticity_token"=>"7z8x8DSXlrYqAgrt7ou65/Q3s4wfenaEInpFgY7Xpeg=", "id"=>"1"}
Completed 401 Unauthorized in 90ms
.....
and then some code that signs user in.....
......
Started POST "/users/sign_in" for 127.0.0.1 at 2012-07-09 21:04:26 +0900
Processing by Devise::SessionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7z8x8DSXlrYqAgrt7ou65/Q3s4wfenaEInpFgY7Xpeg=", "user"=>{"username"=>"kibaekr", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."username" = 'kibaekr' LIMIT 1
(0.2ms) BEGIN
(0.3ms) UPDATE "users" SET "last_sign_in_at" = '2012-07-09 12:01:23.068480', "current_sign_in_at" = '2012-07-09 12:04:26.305663', "sign_in_count" = 38, "updated_at" = '2012-07-09 12:04:26.306566', "commit" = '--- []
' WHERE "users"."id" = 2
(1.7ms) COMMIT
Redirected to http://localhost:3000/vote_for_mission_mission?id=1
Completed 302 Found in 193ms
Started GET "/vote_for_mission_mission?id=1" for 127.0.0.1 at 2012-07-09 21:04:26 +0900
aaaaaaaaa below is page a, which should be missions_path
http://localhost:3000/users/sign_in
aaaaaaaaaa below should be votes
http://localhost:3000/vote_for_mission_mission?id=1
sssssssss below is original page that should be missions_path, not user sign in
http://localhost:3000/users/sign_in
http://localhost:3000/users/sign_in
Processing by MissionsController#vote_for_mission as HTML
Parameters: {"id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
Mission Load (0.1ms) SELECT "missions".* FROM "missions" WHERE "missions"."id" = $1 LIMIT 1 [["id", "1"]]
(0.3ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."voteable_id" = 1 AND "votes"."voteable_type" = 'Mission' AND "votes"."voter_type" = 'User' AND "votes"."voter_id" = 2
Redirected to http://localhost:3000/missions
Completed 302 Found in 179ms
Started GET "/missions" for 127.0.0.1 at 2012-07-09 21:04:26 +0900
aaaaaaaaa below is page a, which should be missions_path
http://localhost:3000/users/sign_in
aaaaaaaaaa below should be votes
http://localhost:3000/missions
====> you can notice that page_a changes after the log in.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment