我正在尝试根据本教程在我的应用中的集合/产品上创建一个“赞”按钮:http://teachmetocode.com/screencasts/create-a-like-or-1-button-with-make_flaggable/
我可以很好地遵循它,并且它非常适合用户喜欢的项目以及不喜欢按钮切换辅助方法等。
然而,我希望能够展示出一个“喜欢”一个集合/产品有多少,并且基于自述文件它应该是方法'标记'。因此,例如,对于第一个集合,它应该是:
Collection.first.flaggings
然而,我收到错误:
Collection Load (0.3ms) SELECT "collections".* FROM "collections" LIMIT 1
SystemStackError: stack level too deep
from /Users/Jeff/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/irb/workspace.rb:80
Maybe IRB bug!
我尝试使用Google搜索并在stackoverflow等上搜索答案,但无济于事。我确实理解堆栈级别太深的错误是由于递归/循环调用?但我不确定这在这里是如何适用的。
非常感谢任何帮助!
使用app中的代码编辑:
的routes.rb
showing collections and 'like'
resources :collections do
member do
get 'like'
end
end
Collection.rb
class Collection < ActiveRecord::Base
attr_accessible :name, :description, :image, :remote_image_url
belongs_to :user
has_many :products, dependent: :destroy
has_many :events, dependent: :destroy
mount_uploader :image, ImageUploader
make_flaggable :like
validates :name, presence: true, length: { maximum: 100 }
validates :description, presence: true, length: { maximum: 200 }
validates :image, presence: true
validates :user_id, presence: true
end
users.rb的部分内容
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation, :user_bio,
:shop, :cover_photo, :avatar, :remote_image_url
has_secure_password
mount_uploader :cover_photo, ImageUploader
mount_uploader :avatar, ImageUploader
make_flagger
scope :shop, where(shop: true)
has_many :posts, dependent: :destroy
has_many :relationships, dependent: :destroy,
foreign_key: "follower_id"
has_many :reverse_relationships, dependent: :destroy,
foreign_key: "followed_id",
class_name: "Relationship"
has_many :following, through: :relationships, source: :followed
has_many :followers, through: :reverse_relationships, source: :follower
has_many :collections, dependent: :destroy
Collections_controller.rb
class CollectionsController < ApplicationController
before_filter :authenticate, only: [:create, :destroy]
before_filter :store_location, only: :show
before_filter :current_collection, only: :show
def like
@current_user = current_user
@collection = Collection.find(params[:id])
if @current_user.flagged?(@collection, :like)
# User has liked it, let's UNlike it
@current_user.unflag(@collection, :like)
redirect_back_or root_path
flash[:success] = "You have unliked this collection."
else
# User has not like it yet, let's like it
@current_user.flag(@collection, :like)
redirect_back_or root_path
flash[:success] = "You have liked this collection!"
end
end
def show
@title = "Collection"
collection_user_id = Collection.find(params[:id]).user_id
@user = User.find(collection_user_id)
@product = Product.new
@products = Collection.find(params[:id]).products
@collection = Collection.find(params[:id])
end
def create
@collection = current_user.collections.build(params[:collection])
if @collection.save
flash[:success] = "New collection added."
redirect_to shop_user_path(current_user)
else
flash[:error] = "Sorry, there seems to be an error adding your collection. Why don't you try again?"
redirect_to shop_user_path(current_user)
end
end
def destroy
@collection = current_user.collections.find_by_id(params[:id])
@collection.destroy
flash[:success] = "Collection deleted."
redirect_to shop_user_path(current_user)
end
private
def current_collection
session[:collection_id] = Collection.find(params[:id]).id.to_i
end
end
如果你们需要更多代码,请告诉我。在此先感谢您的帮助!