将当前 post 添加到所选 collection 的控制器功能(has_many :通过关系)与 Rails 5

Controller Function to add current post to selected collection (has_many :through relation) with Rails 5

我正在尝试创建一个 Collections 功能(与 Pinterest Boards 相同),设计师(又名设计用户)可以在其中创建 collections 并将 posts 分配给拥有 collections。 1个post可以属于多个collection,1个collection可以有多个post。所以我通过 集体化 table 创建了 has_many :through 关系。

现在我在 post 显示页面中显示用户的 collection,代码如下

posts/show.html.erb

  <ul>
    <li>Select a collection to add:</li>
    <% @collections.each do |collection| %>
      <li><%= link_to collection.title, add_to_collection(@post) %></li>
    <% end %>
  </ul>

并将当前 post 分配给所选 collection。所以我开始在 post_controller.rb 中编写一个函数,但我无法联系起来。

↓ 这个add_to_collection函数应该怎么写???

controllers/posts_controller.rb

 def add_to_collection
     # I need to save current post to selected collection in here
 end

谢谢!


我收到这个错误


models/collectivizations.rb

class Collectivization < ApplicationRecord
    belongs_to: post
    belongs_to: collection
end

models/post.rb

class Post < ApplicationRecord
  extend FriendlyId
  friendly_id :title, use: [:slugged, :history]

  belongs_to :category
  belongs_to :designer
  has_many :collectivizations
  has_many :collections, through: :collectivizations

  ...

models/collection.rb

class Collection < ApplicationRecord
  belongs_to :designer
  has_many :collectivizations
  has_many :posts, through: :collectivizations
end

您只需要在连接两者的联接 table 中创建一个新行。

def add_to_collection(post, collection)
     Collectivization.create!(post: post, collection: collection)
 end

并且在您的 rails 视图中也将集合作为变量传递

如果您在 config/routes.rb 中:

resources :posts do 
  resources :collections, controller: :post_collections, except: [:create] do 
    post '/', to: :create 
  end
end

您将获得以下路径:

     post_collection POST   /posts/:post_id/collections/:collection_id(.:format)  post_collections#create
    post_collections GET    /posts/:post_id/collections(.:format)                 post_collections#index
 new_post_collection GET    /posts/:post_id/collections/new(.:format)             post_collections#new
edit_post_collection GET    /posts/:post_id/collections/:id/edit(.:format)        post_collections#edit
                     GET    /posts/:post_id/collections/:id(.:format)             post_collections#show
                     PATCH  /posts/:post_id/collections/:id(.:format)             post_collections#update
                     PUT    /posts/:post_id/collections/:id(.:format)             post_collections#update
                     DELETE /posts/:post_id/collections/:id(.:format)             post_collections#destroy
               posts GET    /posts(.:format)                                      posts#index
                     POST   /posts(.:format)                                      posts#create
            new_post GET    /posts/new(.:format)                                  posts#new
           edit_post GET    /posts/:id/edit(.:format)                             posts#edit
                post GET    /posts/:id(.:format)                                  posts#show
                     PATCH  /posts/:id(.:format)                                  posts#update
                     PUT    /posts/:id(.:format)                                  posts#update
                     DELETE /posts/:id(.:format)                                  posts#destroy

然后,您应该创建一个具有 create 操作的 PostCollectionsController,例如:

class PostCollectionsController < ApplicationController

  def create
    @post = Post.find_by(params[:post_id])
    @collection = Collection.find_by(params[:collection_id])
    @post.collections << @collection
    # render something or whatever
  end

end

然后,在 PostController.show 中,您将执行如下操作:

<ul>
  <li>Select a collection to add:</li>
  <% @collections.each do |collection| %>
    <li>
      <%= link_to collection.title, post_collection_path(@post, collection), method: :post %>
    </li>
  <% end %>
</ul>