我正在构建一个应用程序,用户可以在其中创建食谱,查看所有创建的食谱,在成员区域中查看自己的食谱,最后我希望用户在其帐户中添加"收藏夹".
我是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)
我想我可能在路线上遗漏了一些东西,但我不确定.