【发布时间】:2011-02-08 21:30:19
【问题描述】:
假设我有一个显示视频的 Rails 3 应用。用户可以“喜欢”或“不喜欢”视频。此外,他们可以喜欢/不喜欢游戏等其他事物。在整体设计以及如何处理 RESTful 路由方面,我需要一些帮助。
目前,我有一个 Like Class 使用多态设计,以便对象是“可喜欢的”(likeable_id,likeable_type)
我想通过 AJAX (jQuery 1.5) 做到这一点。所以我在想这样的事情:
javascript
// these are toggle buttons
$("likeVideo").click( function() {
$.ajax({
url: "/likes/video/" + video_id,
method: "POST",
....
});
} );
$("likeGame").click( function() {
$.ajax({
url: "/likes/game/" + game_id,
method: "POST",
....
});
} );
导轨控制器
Class Likes < ApplicationController
def video
# so that if you liked it before, you now DON'T LIKE it so change to -1
# or if you DIDN'T like it before, you now LIKE IT so change to 1
# do a "find_or_create_by..." and return JSON
# the JSON returned will notify JS if you now like or dislike so that the
# button can be changed to match
end
def game
# same logic as above
end
end
路线
match "/likes/video/:id" => "likes#video", :as => :likes_video
match "/likes/game/:id" => "likes#game", :as => :likes_game
这个逻辑看起来正确吗?我正在通过 AJAX 进行 POST。从技术上讲,我不应该做 PUT 吗?还是我太挑剔了?
另外,我的控制器使用非标准动词。喜欢video 和game。我应该担心吗?有时我对如何匹配“正确”动词感到困惑。
另一种方法是使用包含类型(游戏或视频)的数据结构发布到 /likes/:id 之类的内容。然后我可以将它包装在控制器中的一个动词中......甚至可能是更新(PUT)。
任何建议将不胜感激。
【问题讨论】:
-
我将关闭它,因为我已经进一步简化了代码。我现在为游戏和视频发布到“/likes/it”。感谢您提供信息。
标签: ruby-on-rails-3 rest jquery