2016-02-21 64 views
1

我正在爲大學中的項目構建應用程序,並試圖實施某種「添加到收藏夾」 - 我的項目方法。我已經爲一個較舊的問題找到了很好的答案(Implement "Add to favorites" in Rails 3 & 4)。我試圖按照這個答案來實現我的方法。但是在我編寫代碼後,我總是收到類型不匹配錯誤。這裏是我的部分代碼:在Ruby on Rails中實現「添加到收藏夾」

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> 

有誰看到我的錯誤或能幫助我嗎?

+2

什麼是錯誤? –

+0

我相信你也應該爲收藏夾/ favorited_by指定'class_name'(或者它是source_type?),我不確定Rails顯然是你的收藏夾/ favorited_by實際上是爲了引用「ProductionOrder」和「用戶實例。 –

+0

錯誤在下面的答案中 – bobafett1

回答

0

我自己修復了它。我錯過了在我的控制器中定義@production_order