我正在開發一個應用程序,用戶可以在其中查找由廚師製作的食譜。我已經實現了「喜歡/不喜歡」的功能,但現在我想給用戶在集合中保存配方的能力。 用戶可以創建多個集合,因此當他想要將集合中的配方保存到集合中時,他應該能夠查看其當前集合或創建一個新集合。在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)
感謝您的幫助,我改變了設置集合的方式,現在它正在工作。 –
偉大的:-)並感謝您的剔。 –