【问题标题】:Implement a "Add to collection" in rails 4在 rails 4 中实现“添加到集合”
【发布时间】: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


    【解决方案1】:

    将食谱和集合之间的关系设置为多对多还是一对多,这有点令人困惑。您的文字暗示一个集合有许多食谱,一个食谱属于一个集合。虽然您的代码通过使用连接表并声明一个配方具有许多配方集合,这意味着您正在建立多对多关系。

    也就是说你有点搞混了……

    如果一个配方属于一个集合。摆脱连接表。食谱belongs_to :collection,而Collection has_many :recipes。摆脱这两个模型中的其他关系内容。在 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

    希望这会有所帮助。

    【讨论】:

    • 感谢您的帮助,我更改了设置集合的方式,现在可以正常工作了。
    猜你喜欢
    • 2018-10-24
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2021-03-31
    • 1970-01-01
    相关资源
    最近更新 更多