开发者

best way to handle bookmarking in rails

I wanted to get an idea of how to do this properly.

Say I have a photos model and I want users to be able to bookmark them ... so I have a bookmarks model that belongs to both users and photos. What is the best way to implement the creation of the bookmark?

Should I have a bookmarks controller with a create method and just use that ... or should I add a bookmark method to the photos controller and just do the bookmark creation there?

I've done it both ways before, but I wanted to get a feel for what other folk开发者_运维百科s think. I like the bookmark create method idea, but I don't like that I have to construct a POST with variables just to do bookmark something.


You can do either - this is a personal preference thing. I would decide based on this criteria: Do you want to show the object by itself on a page, are there views just for bookmarks? For example, do you plan to have a URL like http://...../users/12/bookmarks to display all bookmarks of a user? If on the other hand you only use the bookmarks on other pages I'd add the methods to add/delete bookmarks to that controller. If you want to add it to more than one you may use a module to avoid duplicating the code. Myself, I recently went THAT route, I only had a create- and a destroy-function for bookmarks and don't display them by themselves, but only in bigger context. As is usual for create/destroy functions, neither has a view, they both redirect back to the item#show page where they were called from (in my app).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜