在 Rails Ruby 中实施“添加到收藏夹”

Implement “Add to favorites” in Ruby on Rails

我正在为一个大学项目构建应用程序,我正在尝试为我的项目实施某种 "Add to favorites" 方法。我已经为一个较旧的问题 (Implement "Add to favorites" in Rails 3 & 4) 找到了一个很好的答案。我尝试按照此答案实施我的方法。但是在我编写代码之后,我总是收到类型不匹配错误。这是我的代码部分:

class User < ActiveRecord::Base
  has_many :production_orders
  has_many :favorite_production_orders
  has_many :favorites, through: :favorite_production_orders, source: :production_order
  ...
end

class ProductionOrder < ActiveRecord::Base
  ...
  belongs_to :user
  has_many :favorite_production_orders
  has_many :favorited_by, through: :favorite_production_orders, source: :user

  ...
end

class FavoriteProductionOrder < ActiveRecord::Base
  belongs_to :user
  belongs_to :production_order
end

Rails.application.routes.draw do
...
resources :production_orders do
    put :favorite, on: :member
  end
...
end

class ProductionOrdersController < ApplicationController
...
def favorite
    type = params[:type]
    if type == "favorite"
      current_user.favorites << @production_order
      redirect_to :back, notice: 'You favorited #{@production_order.number}'

    elsif type == "unfavorite"
      current_user.favorites.delete(@production_order)
      redirect_to :back, notice: 'Unfavorited #{@production_order.number}'

    else
      # Type missing, nothing happens
      redirect_to :back, notice: 'Nothing happened.'
    end
  end
...
end

我的视图有 table 个生产订单

<%- model_class = ProductionOrder -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.titleize %></h1>
</div>
<table class="table table-striped">
  <thead>
    <tr>
      <th><%= model_class.human_attribute_name(:number) %></th>
      <th><%= model_class.human_attribute_name(:article) %></th>
      <th><%= model_class.human_attribute_name(:quantity) %></th>
      <th><%= model_class.human_attribute_name(:pending_quantity) %></th>
      <th><%= model_class.human_attribute_name(:due_date) %></th>
      <th><%= model_class.human_attribute_name(:isCompleted) %></th>
      <th></th>
    </tr>
  </thead>
  <tbody>
    <% @production_orders.each do |production_order| %>
      <tr>
        <td><%= link_to production_order.number, edit_production_order_path(production_order) %></td>
        <td><%= production_order.article.display_name %></td>
        <td><%= production_order.quantity %></td>
        <td><%= production_order.pending_quantity %></td>
        <td><%=l production_order.due_date %></td>
        <td><%= production_order.isCompleted %></td>
        <td>
          <% if current_user %>
              <%= link_to "favorite",   favorite_production_order_path(production_order, type: "favorite"), method: :put %>
              <%= link_to "unfavorite", favorite_production_order_path(production_order, type: "unfavorite"), method: :put %>
          <% end %>
        </td>
        <td>
          <%= link_to ("<span class=\"glyphicon glyphicon-trash\" ></span>").html_safe,
                      production_order_path(production_order),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-xs btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

有没有人看到我的错误或可以帮助我?

我自己修好了。我错过了在我的控制器中定义 @production_order