我有一个从动态生成的 JSON 构建的时间线。应该发生的是它遍历给定用户的事件,并且时间线填充了这些事件。
当我以 current_user 身份访问它时,它工作正常。我修改了代码以使用@access_user 而不是 current_user,这样我就可以在以该用户的版主身份登录时访问它。
在这种情况下,应用程序尝试使用 ID 为 21 的事件填充时间线(不存在 - 21 是事件用户的 ID)。
无论我是以用户还是版主等身份登录,这现在都不起作用 - 所以这是@access_user 正在创建的问题,我猜。
谁能告诉我为什么会这样,我该如何解决?谢谢!
调用的 JS 初始化时间线:
$(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'
});
});
这会正确处理到 /users/21/events.json?callback
这就是 @access_user 的设置方式:
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.id %>,@access_user 似乎工作正常。
这是应该填充时间线的 json.json_builder 文件:
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
这是事件控制器中的 myevents 操作:
def myevents
@events = @access_user.events
respond_with @users
end
和痕迹:
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'
.
.
.
更新:routes.rb 的相关部分:
resources :events do
member do
get 'myevents'
end
end
match 'users/:id/events' => 'events#myevents'
更新:以访客身份登录时的新跟踪(将成员更改为路由上的集合后)用户 ID=2,访客 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):