Ruby on rails Make_flaggable方法“flaggings”返回堆栈级别太深错误
我正试图根据本教程在我的应用程序中的集合/产品上创建一个“like”按钮: 我可以很好地理解它,它可以很好地满足用户对项目的喜好,并且可以通过按钮切换帮助器方法取消链接,等等 然而,我希望能够显示一个集合/产品有多少“喜欢”,并且基于自述文件,应该是“标记”方法。例如,对于第一个系列,它应该是:Ruby on rails Make_flaggable方法“flaggings”返回堆栈级别太深错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正试图根据本教程在我的应用程序中的集合/产品上创建一个“like”按钮: 我可以很好地理解它,它可以很好地满足用户对项目的喜好,并且可以通过按钮切换帮助器方法取消链接,等等 然而,我希望能够显示一个集合/产品有多少“喜欢”,并且基于自述文件,应该是“标记”方法。例如,对于第一个系列,它应该是: Collection.first.flaggings 但是,我得到一个错误: Collection Load (0.3ms) SELECT "collections".* FROM "collect
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!
我试着在谷歌上搜索stackoverflow等的答案,但没用。我知道堆栈级别太深的错误是由递归/循环调用引起的?但我不确定这是怎么回事
非常感谢您的帮助
使用应用程序中的代码编辑:
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\u 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
如果你们还需要代码,一定要告诉我。提前感谢您的帮助 你能粘贴你的模型吗。您自己的代码中可能有一些内容,它返回的mysql语句没有问题。你重启了控制台等吗?@shingara:粘贴了模型。我只是按照说明在user.rb模型中添加make_flagger,并在可以标记的模型中添加make_flaggable,:属性。在本例中,使_可标记:与集合类似。谢谢你们的帮助@同位素:是的,我已经重新启动了服务器、控制台等。谢谢你们的帮助!我想您已经运行了bundle来获取make_的所有依赖项?好像有很多。你能粘贴你的模型吗。您自己的代码中可能有一些内容,它返回的mysql语句没有问题。你重启了控制台等吗?@shingara:粘贴了模型。我只是按照说明在user.rb模型中添加make_flagger,并在可以标记的模型中添加make_flaggable,:属性。在本例中,使_可标记:与集合类似。谢谢你们的帮助@同位素:是的,我已经重新启动了服务器、控制台等。谢谢你们的帮助!我想您已经运行了bundle来获取make_的所有依赖项?似乎有不少。