【发布时间】:2015-04-06 16:20:14
【问题描述】:
我正在开发一个应用程序,用户可以在其中找到厨师制作的食谱。我已经实现了“喜欢/不喜欢”的功能,但现在我想让用户能够将食谱保存在集合中。 用户可以创建多个集合,因此当他想将一个食谱保存到集合中时,他应该能够看到他当前的集合或创建一个新集合。
这是我的模型:
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 %>
我的路线.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)
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 associations