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

时间:2016-02-21 12:46:36

标签: ruby-on-rails ruby 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

带有ProductionOrders表的我的视图

<%- 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>

有人看到我的错误还是可以帮助我?

1 个答案:

答案 0 :(得分:0)

我自己修理了它。我错过了在我的Controller中定义@production_order