I am building an application for a project in university and I'm trying to implement some kind of "Add to favorites"-method for my project. I have already found a good answer for an older question (Implement "Add to favorites" in Rails 3 & 4). I tried to implement my method following this answer. But after I wrote my code, I'm always receiving a type mismatch error. Here are my code parts:
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
My View with a table of 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>
Does anyone see my mistake or could help me?
I fixed it on my own. I missed to define @production_order
in my Controller.