影响控制器的模型中未初始化的常量

时间:2015-12-14 17:50:56

标签: ruby-on-rails ruby model controller

我正在开发一个测试应用程序,在这个应用程序中,有人喜欢的东西会成为该人的订阅者。

控制器:

  def favorite_subscribe
    @favorite_subscription = FavoriteSubscription.add_favorite_subscription(current_user, @user)
    @user.favorite_subscriber_total = @user.favorite_subscriber_total + 1
    @user.save
    redirect_to :back, :notice => "Congratulations, you have favorited #{@user.username}."
  end

模特:

def self.add_favorite_subscription(user, favorite_subscribe)
  user.favorite_subscriptions.where(:subscribe_id => subscribe.id).first_or_create
end

  # Remove the favorite of the user with the other user
  def self.remove_favorite_subscription(user, favorite_subscribe)
    user.favorite_subscriptions.where(:subscribe_id => subscribe.id).destroy_all
  end

  # Get the user with the subscription
  def favorite_subscribe
    User.find(subscribe_id)
  end

我收到一个错误,它无法自动加载常量,并且它希望我的模型定义它。如果有人能提供帮助,那将非常感激。

错误在这里,抱歉:

Unable to autoload constant FavoriteSubscription, expected /home/jakxna360/rails/test/app/models/favorite_subscription.rb to define it 

1 个答案:

答案 0 :(得分:1)

This usually means that Rails is unable to find the file in which some class is defined and therefore autoloading fails. Rails is very strict about its conventions.

In the context of the posted code, I suggest double checking that

  1. the class FavoriteSubscription (singular) is defined in a file named app/models/favorite_subscription.rb (singular), that a database table is named favorite_subscriptions (plural) and that is exists.
  2. the FavoriteSubscriptionsController (plural) is defined in a file named app/controllers/favorite_subscriptions_controller.rb (plural).
相关问题