我正在构建一个应用程序,用户可以在其中创建食谱,查看所有创建的食谱,在成员区域中查看自己的食谱,最后我希望用户在其帐户中添加"收藏夹".
我是Rails的新手但阅读了文档,这是我对后端应该是什么样子的理解.有人可以确认这看起来正确或建议任何错误,并解释我是否做错了(可能是这种情况)?
所以这是我的代码:
用户模型
has_many :recipes
has_many_favorites, :through => :recipes
Run Code Online (Sandbox Code Playgroud)
食谱模型
belongs_to :user
has_many :ingredients #created seperate db for ingredients
has_many :prepererations #created seperate db for prep steps
Run Code Online (Sandbox Code Playgroud)
喜欢的模特
belongs_to :user
has_many :recipes, :through => :user
#this model has one column for the FK, :user_id
Run Code Online (Sandbox Code Playgroud)
收藏控制器
def create
@favrecipes =current_user.favorites.create(params[:user_id])
end
Run Code Online (Sandbox Code Playgroud)
然后我想要一个按钮发布到数据库,所以我有这个:
<%= button_to("Add to Favorites" :action => "create", :controller => "favorites" %>
Run Code Online (Sandbox Code Playgroud)
我想我可能在路线上遗漏了一些东西,但我不确定.
Tho*_*emm 66
您描述的特定设置混合了几种类型的关联.
首先,我们有一个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
Run Code Online (Sandbox Code Playgroud)
接下来,我们需要决定如何实现用户应该能够标记喜欢的食谱的故事.
这可以通过使用连接模型来完成 - 让我们称之为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
Run Code Online (Sandbox Code Playgroud)
添加此收藏夹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
Run Code Online (Sandbox Code Playgroud)
##
# 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)
Run Code Online (Sandbox Code Playgroud)
如何实现Controller动作和路由可能有几种方法.我非常喜欢Ryan Bates在ActiveRecord声誉系统的Railscast#364中所展示的那个.下面描述的解决方案的一部分沿着那里的投票上下机制构成.
在我们的Routes文件中,我们在名为favorite的食谱中添加成员路径.它应该响应发布请求.这将为我们的视图添加favorite_recipe_path(@recipe)url帮助器.
# config/routes.rb
resources :recipes do
put :favorite, on: :member
end
Run Code Online (Sandbox Code Playgroud)
在我们的RecipesController中,我们现在可以添加相应的收藏操作.在那里,我们需要确定用户想要做什么,喜欢还是不喜欢.为此,可以引入一个名为eg type的请求参数,我们也必须稍后将其传递给我们的链接助手.
class RecipesController < ...
# Add and remove favorite recipes
# for current_user
def favorite
type = params[:type]
if type == "favorite"
current_user.favorites << @recipe
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
Run Code Online (Sandbox Code Playgroud)
在您的视图中,您可以将相应的链接添加到收藏和不受欢迎的食谱中.
<% 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 %>
Run Code Online (Sandbox Code Playgroud)
而已.如果用户单击配方旁边的"收藏夹"链接,则此配方将添加到current_user的收藏夹中.
我希望有所帮助,请问你喜欢的任何问题.
关于协会的Rails指南非常全面,在开始时会对你有很大的帮助.
| 归档时间: |
|
| 查看次数: |
9489 次 |
| 最近记录: |