简体   繁体   中英

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 folks 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM