所以我试图为我的应用程序实现一个favouriting系统并遵循本指南:
您描述的特定设置混合了几种类型的关联。
首先,我们有一个User模型,第二个是Recipe模型。每个食谱属于一个用户,因此我们有一个User:has_many食谱,Recipe belongs_to:用户关联。此关系存储在配方的user_id字段中。
$ rails g model Recipe user_id:integer ...
$ rails g model User ...
class Recipe < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :recipes
end
接下来,我们需要决定如何实现用户应该能够标记喜欢的食谱的故事。
这可以通过使用连接模型来完成 - 让我们称之为FavoriteRecipe - 使用列:user_id和:recipe_id。我们在这里建立的关联是 has_many:通过关联。
A User
- has_many :favorite_recipes
- has_many :favorites, through: :favorite_recipes, source: :recipe
A Recipe
- has_many :favorite_recipes
- has_many :favorited_by, through: :favorite_recipes, source: :user
# returns the users that favorite a recipe
添加此收藏夹has_many:通过与模型关联,我们得到了最终结果。
$ rails g model FavoriteRecipe recipe_id:integer user_id:integer
# Join model connecting user and favorites
class FavoriteRecipe < ActiveRecord::Base
belongs_to :recipe
belongs_to :user
end
---
class User < ActiveRecord::Base
has_many :recipes
# Favorite recipes of user
has_many :favorite_recipes # just the 'relationships'
has_many :favorites, through: :favorite_recipes, source: :recipe # the actual recipes a user favorites
end
class Recipe < ActiveRecord::Base
belongs_to :user
# Favorited by users
has_many :favorite_recipes # just the 'relationships'
has_many :favorited_by, through: :favorite_recipes, source: :user # the actual users favoriting a recipe
end
##
# Association "A"
# Find recipes the current_user created
current_user.recipes
# Create recipe for current_user
current_user.recipes.create!(...)
# Load user that created a recipe
@recipe = Recipe.find(1)
@recipe.user
##
# Association "B"
# Find favorites for current_user
current_user.favorites
# Find which users favorite @recipe
@recipe = Recipe.find(1)
@recipe.favorited_by # Retrieves users that have favorited this recipe
# Add an existing recipe to current_user's favorites
@recipe = Recipe.find(1)
current_user.favorites << @recipe
# Remove a recipe from current_user's favorites
@recipe = Recipe.find(1)
current_user.favorites.delete(@recipe) # (Validate)
如何实现Controller动作和路由可能有几种方法。我非常喜欢Ryan Bates在ActiveRecord信誉系统Railscast #364中所展示的那个。下面描述的解决方案的一部分是根据那里的投票上下机制构建的。
在我们的路线文件中,我们在名为收藏夹的食谱上添加成员路线。它应该响应发布请求。这将为我们的视图添加favorite_recipe_path(@recipe)url帮助器。
# config/routes.rb
resources :recipes do
put :favorite, on: :member
end
在我们的RecipesController中,我们现在可以添加相应的收藏夹操作。在那里,我们需要确定用户想要做什么,喜欢还是不喜欢。为此,请求参数称为例如可以引入类型,我们以后也必须将它们传递给我们的链接助手。
class RecipesController < ...
# Add and remove favorite recipes
# for current_user
def favorite
type = params[:type]
if type == "favorite"
current_user.favorites << @recipe #stuck here :(
redirect_to :back, notice: 'You favorited #{@recipe.name}'
elsif type == "unfavorite"
current_user.favorites.delete(@recipe)
redirect_to :back, notice: 'Unfavorited #{@recipe.name}'
else
# Type missing, nothing happens
redirect_to :back, notice: 'Nothing happened.'
end
end
end
在您的视图中,您可以将相应的链接添加到收藏和不受欢迎的食谱中。
<% if current_user %>
<%= link_to "favorite", favorite_recipe_path(@recipe, type: "favorite"), method: :put %>
<%= link_to "unfavorite", favorite_recipe_path(@recipe, type: "unfavorite"), method: :put %>
<% end %>
就是这样。如果用户单击配方旁边的“收藏夹”链接,则此配方将添加到current_user的收藏夹中。
我希望有所帮助,请问您喜欢的任何问题。
Rails guides on associations非常全面,在开始使用时会给你很多帮助。
Thomas Klemm发表于Implement "Add to favorites" in Rails 3 & 4 但我一直收到一个错误: 未初始化的常量User :: FavoriteRecipe
一些帮助将不胜感激
现在它突出了这一行: current_user.favorites&lt;&lt; @recipe(这是发生错误的地方(在控制器中))