2015-04-06 28 views
0

我正在開發一個應用程序,用戶可以在其中查找由廚師製作的食譜。我已經實現了「喜歡/不喜歡」的功能,但現在我想給用戶在集合中保存配方的能力。 用戶可以創建多個集合,因此當他想要將集合中的配方保存到集合中時,他應該能夠查看其當前集合或創建一個新集合。在rails中實現「添加到集合」4

這裏是我的模型:

class User < ActiveRecord::Base 
has_many :collections 
accepts_nested_attributes_for :collections 
end 

class Collection < ActiveRecord::Base 
has_many :recipe_collections 
has_many :recipes, through: :recipe_collections 
end 

class Recipe < ActiveRecord::Base 
has_many :recipe_collections 
end 

class RecipeCollection < ActiveRecord::Base 
belongs_to :recipe 
belongs_to :collection 
end 

我recipe_collections_controller.rb

class RecipeCollectionsController < ApplicationController 
    before_action :set_recipe, only: [:create] 
    before_action :set_user, only: [:create] 


    def create 
    @recipe_collection = @recipe.recipe_collections.build(recipe_collection_params) 
    if current_user 
     @recipe_collection.save 
     respond_to do |format| 
     format.html {redirect_to :back } 
     end 
    else 
     respond_to do |format| 
     format.html { render 'recipes/show' } 
     end 
    end 
    end 

    private 

    def set_user 
    @user = current_user 
    end 

    def set_recipe 
    @recipe = Recipe.find(params[:recipe_id]) 
    end 

    def recipe_collection_params 
    params.require(:recipe_collection).permit(:collection_id, :recipe_id, collection_attributes: [:id], recipe_attributes: [:id]) 
    end 

end 

在配方節目,我有一個渲染,show.html.erb:

<%= render "recipe_collections/form", collection: @recipe_collection || @recipe.recipe_collections.build%> 

我的部分_form.html.erb

<%= simple_form_for ([ collection.recipe, collection ]) do |form| %> 
    <%= form.association :collection, as: :check_boxes %> 
    <%= form.button :submit %> 
<% end %> 

我的routes.rb

resources :recipes, :concerns => :paginatable do 
    member do 
     get "like", to: "recipes#upvote" 
     get "dislike", to: "recipes#downvote" 
    end 
    resources :reviews, only: :create 
    resources :recipe_collections 
    end 

是什麼問題:

1:在我的節目我有一個複選框形式,但它不顯示當前用戶的集合,但所有。

2:當我提交表單時,它不會將配方保存在我選擇的集合中。

編輯:

這裏是我提交表格時的日誌的例子。

Started POST "/recipes/66/recipe_collections" for ::1 at 2015-04-06 23:58:06 +0200 
Processing by RecipeCollectionsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"GhMIJs4nNmwsLpzBj4l5ta/OW6fN9dfuzBBnciJsjEa0YlfjQMQYDEmwhcXb9++oEmS36yEbgICNsSdbLgiigA==", "recipe_collection"=>{"collection_id"=>["24", ""]}, "commit"=>"Créer un(e) Recipe collection", "recipe_id"=>"66"} 
    (0.4ms) SELECT COUNT(*) FROM "recipes" 
    (0.2ms) SELECT COUNT(*) FROM "publishers" 
    (0.2ms) SELECT COUNT(*) FROM "votes" 
    Recipe Load (0.2ms) SELECT "recipes".* FROM "recipes" WHERE "recipes"."id" = $1 LIMIT 1 [["id", 66]] 
    User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 5]] 
Unpermitted parameter: collection_id 
    (0.1ms) BEGIN 
    SQL (0.3ms) INSERT INTO "recipe_collections" ("recipe_id", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["recipe_id", 66], ["created_at", "2015-04-06 21:58:06.566948"], ["updated_at", "2015-04-06 21:58:06.566948"]] 
    (0.4ms) COMMIT 
Redirected to http://localhost:3000/recipes/66 
Completed 302 Found in 11ms (ActiveRecord: 2.0ms) 

回答

0

無論您是將食譜和收藏之間的關係設置爲多對多還是一對多,它都有點令人困惑。你的文字暗示一個集合有很多食譜,一個食譜屬於一個集合。雖然你的代碼,通過使用連接表和陳述一個配方有許多配方集合意味着你正在建立一個多對多的關係。

換句話說,你是有點混了......

如果食譜屬於集合。擺脫連接表。食譜belongs_to:收集,而集合has_many:食譜。擺脫這兩種模式中的其他關係。在Recipe的遷移表中,確保有一個t.belongs_to:collection來設置外鍵。

如果一個配方有,並且屬於許多集合。保留連接表,但將其命名爲collections_recipes,因爲Rails只有在雙方都按照字母順序複數化時才能找到它。食譜has_and_belongs_to_many:集合,集合has_and_belong_to_many:食譜。放下has_many:recipe_collections和through:位,不要替換,Rails會爲你做這件事。在你的移植中檢查你的連接表有t.belongs_to:recipe和:collection

希望這有助於。

+0

感謝您的幫助,我改變了設置集合的方式,現在它正在工作。 –

+0

偉大的:-)並感謝您的剔。 –