0

我正在尝试关注这篇关于将收藏夹添加到我的 Rails 应用程序的帖子:在 Rails 3 和 4 中实现“添加到收藏夹”

在我的应用程序中,我希望用户能够收藏项目。现在,当我尝试在项目控制器中实现 link_to 收藏夹操作时,我收到错误消息:

ActiveRecord::HasManyThroughSourceAssociationNotFoundError in ProjectsController#favorite
Could not find the source association(s) :favorite or :favorites in model FavoriteProject.
Try 'has_many :favorites, :through => :favorite_projects, :source => <name>'. Is it one of :project or :user?

我该如何解决这个错误?

最喜欢的项目.rb:

class FavoriteProject < ActiveRecord::Base
  attr_accessible :project_id, :user_id
  belongs_to :project
  belongs_to :user
end

用户.rb:

class User < ActiveRecord::Base
  extend FriendlyId
  friendly_id :username
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :token_authenticatable, :confirmable, :lockable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :avatar
  # attr_accessible :title, :body

  has_many :projects, :dependent => :destroy

  # Favorite projects
  has_many :favorite_projects # just the 'relationships'
  has_many :favorites, :through => :favorite_projects # projects a user favorites

  accepts_nested_attributes_for :projects

  mount_uploader :avatar, AvatarUploader

  validates :username, :presence => true
  validates :email, :presence => true
end

项目.rb:

class Project < ActiveRecord::Base
  attr_accessible :title, :images_attributes, :ancestry, :user_id
  has_many :steps, :dependent => :destroy
  has_many :images, :dependent => :destroy

  belongs_to :user

   # Favorited by users
  has_many :favorite_projects
  has_many :favorited_by, :through => :favorite_projects, :source => :user # users that favorite a project

  accepts_nested_attributes_for :steps
  accepts_nested_attributes_for :images

  validates :title, :presence => true
end

项目控制器.rb

 def favorite
    current_user.favorites << @project
    redirect_to :back
  end

路线.rb

Build::Application.routes.draw do

  devise_for :users, :controllers => {:registrations => :registrations}
  get "home/index"

  resources :users do
     match 'users/:id' => 'users#username'
  end

  resources :projects do
     collection {post :sort}
     get :editTitle, on: :collection
     put :favorite, on: :member
    resources :steps do
        collection {post :sort}
        get :create_branch_a, on: :collection
        get :create_branch_b, on: :collection
        get :update_ancestry, on: :collection
        get :edit_redirect, on: :collection
        get :show_redirect, on: :collection
      end
      match "steps/:id" => "steps#number", :as => :number
  end

  resources :images do
     collection {post :sort}
   end

  root :to => 'home#index'

  post "versions/:id/revert" => "versions#revert", :as => "revert_version"

end

index.html.erb 视图文件:

 <%= link_to "", favorite_project_path(@project), method: :put, :class=> "icon-star-empty favoriteStar", :title=> "Favorite", :rel=>"tooltip" %> 
4

1 回答 1

1

你有

has_many :favorites, :through => :favorite_projects

但是模型中没有favorites关联FavoriteProject。只有belongs_to :project. 它应该是:

has_many :favorites, :through => :favorite_projects, :source => :project
于 2013-06-01T20:02:06.133 回答