Rails - 尋找錯誤 ID 的 JSON 回調 (Rails - JSON callback looking for wrong id)


問題描述

Rails ‑ 尋找錯誤 ID 的 JSON 回調 (Rails ‑ JSON callback looking for wrong id)

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

<p>@access_user seems to work fine if I call <%= @access_user.id %> in a view.</p>

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

參考解法

方法 1:

After seeing the routes, I think I know what's going on. You set the routes "member" instead of routes "collection". As a "member" it will always attempt to search for the Event defined in the id (an id that, in this case because of the match clause in your routes, is actually coming from the User). Change your routes to:

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

There's a nice answer here: difference between collection route and member route in ruby on rails?

(by ecsGSP)

參考文件

  1. Rails ‑ JSON callback looking for wrong id (CC BY‑SA 3.0/4.0)

#controller #methods #ruby-on-rails #JSON






相關問題

覆蓋我的控制器,我應該在哪個事件中檢查 cookie? (Overriding my controller, which event should I check for a cookie in?)

ASP.NET MVC,將模型從視圖傳遞到控制器 (ASP.NET MVC, passing Model from View to Controller)

Ruby on Rails:更改列表順序後未顯示錯誤消息 (Ruby on Rails: Error messages not displaying since changing order of a list)

Успадкоўванне кантролера ASP.NET MVC 4 (ASP.NET MVC 4 controller inheritance)

如何在 CakePHP 中創建自定義 MySQL 查詢? (How to create custom MySQL queries in CakePHP?)

Respond_to 沒有調用正確的格式 (Respond_to does not call the right format)

Rails - 尋找錯誤 ID 的 JSON 回調 (Rails - JSON callback looking for wrong id)

YIi PHP - 帶有 foreach 循環的輸出數組 (YIi PHP - Output array with a foreach loop)

使用 SQL 將方法邏輯移動到 Rails 項目中的模型 (Moving method logic with SQL to model in rails project)

MVC 為什麼要單元測試控制器 (MVC Why unit test controllers)

Jquery & Rails 3:從 js 文件運行控制器操作? (Jquery & Rails 3: Run controller action from js file?)

.net core mvc:在區域上添加具有 crud 功能的控制器未正確生成代碼和鏈接 (.net core mvc: adding controller with crud functionality on area didn't generate code and link correctly)







留言討論