为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地6264312 2019-11-26 13:19:23
我正在构建一个应用程序,用户可以在其中创建配方,查看所有创建的配方,在成员区域中查看自己的配方,最后我希望用户向其帐户添加“收藏夹”。我是Rails的新手,但已经阅读了文档,这是我对后端外观的理解。如果我做错了事(可能是这种情况),可以请人确认这看起来正确还是建议任何错误,并附上解释?这是我的代码:用户模型has_many :recipeshas_many_favorites, :through => :recipes配方模型belongs_to :userhas_many :ingredients #created seperate db for ingredientshas_many :prepererations #created seperate db for prep steps最喜欢的模型belongs_to :userhas_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" %>我认为我可能在路线中缺少一些东西,但不确定。
查看完整描述

3 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

这个线程超级有帮助!!


谢谢!


不要忘记在表单标签中包含=。


<% 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 %>


查看完整回答
反对 回复 2019-11-26
  • 3 回答
  • 0 关注
  • 652 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信