在這之前,我已經做了幾個文章,關於如何添加一個喜歡的食譜給用戶..我有一個應用程序,您可以在登錄後上傳食譜,用戶可以搜索整個所有食譜的表格和查看他們自己的食譜在一個會員區..該模型的參數沒有被退回rails 3
現在我希望用戶能夠保存他們最喜歡的食譜,到目前爲止,我可以保存一個喜歡的食譜,因此,我得到的輸出是
[#<Favourite id: 1, user_id: 8, recipe_id: nil, created_at: "2012-11-06 19:25:34", updated_at: "2012-11-06 19:25:34">,
所以我得到了正確的user_id但沒有params的實際配方,即菜的名稱,原產國。
我的模型,像這樣
用戶
class User < ActiveRecord::Base
has_many :recipes
has_many :favourites
配方
has_many :ingredients
has_many :preperations
has_many :favourites
收藏
belongs_to :user
belongs_to :recipe
我favouri TE控制器看起來像這樣
def create
@favourite = current_user.favourites.new(params[:recipe])
if @favourite.save
redirect_to my_recipes_path, :notice => "Recipe added to Favourites"
end
end
添加到您的收藏夾鏈接
<%= link_to "Add to favorites", {:controller => 'favourites', :action => 'create'}, {:method => :post } %>
我希望我沒有錯過什麼了,任何幫助讚賞
謝謝你的回答,也嘗試過你的方法,得到錯誤消息調用id爲零,這將錯誤地爲4 - 如果你真的想要的ID爲零,使用object_id – Richlewis