我正在尝试关注将这些帖子添加到我的Rails应用中的帖子:Implement "Add to favorites" in 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?
如何解决此错误?
favorite_project.rb:
class FavoriteProject < ActiveRecord::Base
attr_accessible :project_id, :user_id
belongs_to :project
belongs_to :user
end
user.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
project.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
projects_controller.rb
def favorite
current_user.favorites << @project
redirect_to :back
end
的routes.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" %>
答案 0 :(得分:1)
你有
has_many :favorites, :through => :favorite_projects
但favorites
模型中没有FavoriteProject
关联。只有1个}}。它应该是:
belongs_to :project