I've got a timeline that is being built from dynamically generated JSON. What should be happening is that it loops through a given user's events and the timeline is populated with these.
It was working fine when I was accessing it as the current_user. I have modified the code to use @access_user instead of current_user, so that I can access it whilst logged in as that user's moderator.
In this case, the app is trying to populate the timeline with an event with the ID of 21 (which doesn't exist - 21 is the ID of the event's user).
This now doesn't work whether I am logged in as a user or moderator etc - so it's an issue that @access_user is creating I'm guessing.
Can anyone tell me why this is happening, and how I can fix it? Thanks!
JS that calls initializes the timeline:
$(document).ready(function() {
createStoryJS({
type: 'timeline',
width: '820',
height: '600',
source: '/users/<%= @access_user.id %>/events.json?callback',
embed_id: 'my-timeline',
font: 'Euphoria-Droid',
debug: true,
gmap_key: '#',
maptype: 'HYBRID'
});
});
This processes correctly to /users/21/events.json?callback
This is how @access_user is being set:
def access_user
if user_signed_in?
@access_user = current_user
elsif keyholder_signed_in?
@access_user = current_keyholder.user
elsif guest_signed_in?
@access_user = current_guest.user
end
end
@access_user seems to work fine if I call <%= @access_user.id %> in a view.
This is the json.json_builder file that should be populating the timeline:
timeline do
headline @access_user.first_name
type "default"
text "A Timeline"
startDate ""
date @events do |event|
startDate event.start_date.strftime('%Y,%m,%d')
endDate ""
headline event.headline
text event.text
asset do
media event.media
caption event.caption
end
end
end
And this is the myevents action in the events controller:
def myevents
@events = @access_user.events
respond_with @users
end
And the trace:
Started GET "/users/21/events.json?callback?callback=onJSONP_Data" for 127.0.0.1
at 2013-08-20 13:09:28 +0100
Processing by EventsController#myevents as JSON
Parameters: {"callback?callback"=>"onJSONP_Data", "id"=>"21"}
←[1m←[35mKeyholder Load (2.0ms)←[0m SELECT "keyholders".* FROM "keyholders" W
HERE "keyholders"."id" = 5 LIMIT 1
←[1m←[36mUser Load (3.0ms)←[0m ←[1mSELECT "users".* FROM "users" WHERE "users
"."id" = 21 LIMIT 1←[0m
←[1m←[35mEvent Load (5.0ms)←[0m SELECT "events".* FROM "events" WHERE "events
"."id" = ? LIMIT 1 [["id", "21"]]
Completed 404 Not Found in 51ms
ActiveRecord::RecordNotFound (Couldn't find Event with id=21):
activerecord (3.2.14) lib/active_record/relation/finder_methods.rb:344:in `fin
d_one'
.
.
.
Update: Relevant parts of routes.rb:
resources :events do
member do
get 'myevents'
end
end
match 'users/:id/events' => 'events#myevents'
Update: new trace when logged in as a guest (after changing member to collection on routes) User ID=2, guest ID=2:
Started GET "/timelines/4" for 127.0.0.1 at 2013-08-20 16:33:41 +0100
Processing by TimelinesController#show as HTML
Parameters: {"id"=>"4"}
←[1m←[36mGuest Load (2.0ms)←[0m ←[1mSELECT "guests".* FROM "guests" WHERE "gu
ests"."id" = 2 LIMIT 1←[0m
←[1m←[35mUser Load (10.0ms)←[0m SELECT "users".* FROM "users" WHERE "users"."
id" = 2 LIMIT 1
←[1m←[36mTimeline Load (1.0ms)←[0m ←[1mSELECT "timelines".* FROM "timelines"
WHERE "timelines"."id" = ? LIMIT 1←[0m [["id", "4"]]
Rendered timelines/_show_timeline.html.erb (1.0ms)
Rendered timelines/_my_timeline.html.erb (1.0ms)
Rendered timelines/_new_event.html.erb (339.0ms)
←[1m←[35mEvent Load (2.0ms)←[0m SELECT "events".* FROM "events" INNER JOIN "t
imelines" ON "events"."timeline_id" = "timelines"."id" WHERE "timelines"."user_i
d" = 2
←[1m←[36mEXPLAIN (0.0ms)←[0m ←[1mEXPLAIN QUERY PLAN SELECT "events".* FROM "e
vents" INNER JOIN "timelines" ON "events"."timeline_id" = "timelines"."id" WHERE
"timelines"."user_id" = 2←[0m
EXPLAIN for: SELECT "events".* FROM "events" INNER JOIN "timelines" ON "events".
"timeline_id" = "timelines"."id" WHERE "timelines"."user_id" = 2
0|0|0|SCAN TABLE events (~1000000 rows)
0|1|1|SEARCH TABLE timelines USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
Rendered timelines/_edit_events.html.erb (1034.1ms)
Rendered timelines/_delete_events.html.erb (5.0ms)
Rendered timelines/show.html.erb within layouts/application (3915.2ms)
←[1m←[35mFuneral Load (4.0ms)←[0m SELECT "funerals".* FROM "funerals" WHERE "
funerals"."user_id" = 2 LIMIT 1
←[1m←[36mTimeline Load (1.0ms)←[0m ←[1mSELECT "timelines".* FROM "timelines"
WHERE "timelines"."user_id" = 2 LIMIT 1←[0m
←[1m←[35mKeyholder Load (2.0ms)←[0m SELECT "keyholders".* FROM "keyholders" W
HERE "keyholders"."user_id" = 2 LIMIT 1
Rendered partials/_guest_options.html.erb (395.0ms)
Completed 200 OK in 12947ms (Views: 9729.6ms | ActiveRecord: 133.0ms)
Started GET "/users/2/events.json?callback?callback=onJSONP_Data" for 127.0.0.1
at 2013-08-20 16:33:58 +0100
Processing by EventsController#myevents as JSON
Parameters: {"callback?callback"=>"onJSONP_Data", "id"=>"2"}
←[1m←[36mGuest Load (17.0ms)←[0m ←[1mSELECT "guests".* FROM "guests" WHERE "g
uests"."id" = 2 LIMIT 1←[0m
←[1m←[35mUser Load (3.0ms)←[0m SELECT "users".* FROM "users" WHERE "users"."i
d" = 2 LIMIT 1
←[1m←[36mEvent Load (3.0ms)←[0m ←[1mSELECT "events".* FROM "events" WHERE "ev
ents"."id" = ? LIMIT 1←[0m [["id", "2"]]
Completed 404 Not Found in 149ms
ActiveRecord::RecordNotFound (Couldn't find Event with id=2):