Rails - 寻找错误id的JSON回调

时间:2013-08-20 12:11:25

标签: ruby-on-rails json methods controller

我有一个由动态生成的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
如果我调用&lt;%= @ access_user.id%&gt;

@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):

1 个答案:

答案 0 :(得分:0)

看到路线后,我想我知道发生了什么事。您设置路线“成员”而不是路线“集合”。作为“成员”,它总是会尝试搜索id中定义的事件(在这种情况下,由于路由中的匹配子句,ID实际上来自用户)。将您的路线更改为:

resources :events do
  collection do
    get 'myevents'
  end
end

这里有一个很好的答案:difference between collection route and member route in ruby on rails?