在Rails 3和4中实施“添加到收藏夹”

我正在构建一个应用程序,用户可以在其中创建配方,查看所有创建的配方,在成员区域中查看自己的配方,最后我希望用户向其帐户添加“收藏夹”。


我是Rails的新手,但已经阅读了文档,这是我对后端外观的理解。如果我做错了事(可能是这种情况),可以请人确认这看起来正确还是建议任何错误,并附上解释?


这是我的代码:


用户模型


has_many :recipes

has_many_favorites, :through => :recipes

配方模型


belongs_to :user

has_many :ingredients #created seperate db for ingredients

has_many :prepererations #created seperate db for prep steps

最喜欢的模型


belongs_to :user

has_many :recipes, :through => :user

#this model has one column for the FK, :user_id

收藏夹控制器


def create

  @favrecipes =current_user.favorites.create(params[:user_id])

end

然后,我想有一个按钮发布到数据库,所以我有这个:


<%= button_to("Add to Favorites" :action => "create", :controller => "favorites" %>

我认为我可能在路线中缺少一些东西,但不确定。


慕的地6264312
浏览 661回答 3
3回答

不负相思意

这个线程超级有帮助!!谢谢!不要忘记在表单标签中包含=。<% if current_user %>&nbsp; <%=link_to "favorite",&nbsp; &nbsp;favorite_recipe_path(@recipe, type: "favorite"), method: :put %>&nbsp; <%=link_to "unfavorite", favorite_recipe_path(@recipe, type: "unfavorite"), method: :put %><% end %>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby