在 rails 中实施 "Add to collection" 4
Implement a "Add to collection" in rails 4
我正在开发一款应用程序,用户可以在其中找到厨师制作的食谱。我已经实现了 "like/dislike" 功能,但现在我想让用户能够将食谱保存在 collection 中。
用户可以创建许多 collections,因此当他想在 collection 中保存食谱时,他应该能够看到他当前的 collections 或创建一个新的。
这是我的模型:
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:在我的节目中,我有一个复选框表单,但它不显示当前用户 collection,而是显示所有用户。
2:当我提交表单时,它没有将食谱保存在我选择的collection中。
编辑:
这是我提交表单时的日志示例。
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" = LIMIT 1 [["id", 66]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 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 (, , ) 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)
你把菜谱和collection的关系设置成many-to-many还是one-to-many有点迷惑。您的文字暗示 collection 有很多食谱,而一个食谱属于一个 collection。虽然您的代码通过使用连接 table 并说明食谱有很多 recipe-collections 意味着您正在建立 many-to-many 关系。
也就是说你有点糊涂了...
如果一个菜谱属于collection。去掉加入table。食谱 belongs_to :collection,而 Collection has_many :食谱。摆脱这两个模型中的其他关系。在 Recipe 的迁移 table 中,确保有一个 t.belongs_to :collection 来设置外键。
如果一个食谱拥有并属于许多 collection。 保留连接 table,但将其命名为 collections_recipes Rails只有两边都是复数,并且是按字母顺序排列的,才能找到。食谱有_and_belongs_to_many :collections, Collection has_and_belong_to_many :食谱。删除 has_many :recipe_collections 和 through: 位,不要替换任何一个,Rails 会为您完成此操作。检查您在迁移中加入 table 是否有 t.belongs_to :recipe 和 :collection
希望这对您有所帮助。
我正在开发一款应用程序,用户可以在其中找到厨师制作的食谱。我已经实现了 "like/dislike" 功能,但现在我想让用户能够将食谱保存在 collection 中。 用户可以创建许多 collections,因此当他想在 collection 中保存食谱时,他应该能够看到他当前的 collections 或创建一个新的。
这是我的模型:
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:在我的节目中,我有一个复选框表单,但它不显示当前用户 collection,而是显示所有用户。
2:当我提交表单时,它没有将食谱保存在我选择的collection中。
编辑:
这是我提交表单时的日志示例。
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" = LIMIT 1 [["id", 66]]
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = 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 (, , ) 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)
你把菜谱和collection的关系设置成many-to-many还是one-to-many有点迷惑。您的文字暗示 collection 有很多食谱,而一个食谱属于一个 collection。虽然您的代码通过使用连接 table 并说明食谱有很多 recipe-collections 意味着您正在建立 many-to-many 关系。
也就是说你有点糊涂了...
如果一个菜谱属于collection。去掉加入table。食谱 belongs_to :collection,而 Collection has_many :食谱。摆脱这两个模型中的其他关系。在 Recipe 的迁移 table 中,确保有一个 t.belongs_to :collection 来设置外键。
如果一个食谱拥有并属于许多 collection。 保留连接 table,但将其命名为 collections_recipes Rails只有两边都是复数,并且是按字母顺序排列的,才能找到。食谱有_and_belongs_to_many :collections, Collection has_and_belong_to_many :食谱。删除 has_many :recipe_collections 和 through: 位,不要替换任何一个,Rails 会为您完成此操作。检查您在迁移中加入 table 是否有 t.belongs_to :recipe 和 :collection
希望这对您有所帮助。