我正在开发一个应用程序,用户可以在应用程序中找到厨师制作的菜谱。我已经实现了一个“喜欢/不喜欢”的字体,但是现在我想给用户一个在集合中保存菜谱的能力。用户可以创建许多集合,所以当他想要在一个集合中保存一个菜谱时,他应该能够看到他的当前集合或者创建一个新的集合。
以下是我的模特:
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:在我的演示中,我有一个复选框表单,但它不显示当前的用户集合,而是全部显示。
他说:当我提交表单时,它不会将菜谱保存在我选择的集合中。
编辑:
这里是我提交表单时日志的一个例子。
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)
发布于 2015-04-08 23:56:51
无论你是建立食谱和收藏之间的关系,还是一对多,这有点让人费解。您的文本暗示一个集合有许多菜谱,并且一个菜谱属于一个集合。虽然您的代码,通过使用一个连接表并声明一个菜谱有很多菜谱集合,这意味着您正在建立一个多到多的关系。
换句话说你有点搞混了..。
如果菜谱属于集合,则为。去掉联接表。菜谱belongs_to :集合,而集合has_many :菜谱。摆脱这两种模式中的其他关系。在菜谱迁移表中,确保有一个t.belongs_to :集合来设置外键。
如果一个菜谱有并且属于许多集合,则。保留join表,但是将它命名为Rails,只有当双方都是复数的并且按字母顺序排列时才能找到它。食谱has_and_belongs_to_many :集合,集合has_and_belong_to_many :菜谱。删除has_many :recipe_collections和直通: bit,也不要替换,Rails为您做这件事。检查迁移中的join表有t.belongs_to :食谱和:collection
希望这能有所帮助。
https://stackoverflow.com/questions/29475145
复制相似问题