【问题标题】:Rails Model Association: FavoritingRails 模型协会:收藏
【发布时间】:2013-06-01 19:50:07
【问题描述】:

我正在尝试关注这篇关于将收藏夹添加到我的 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" %> 

【问题讨论】:

  • 添加@project后是否尝试保存current_user?
  • 我不确定我为什么需要这样做。你能进一步解释一下吗?
  • 在控制台中试试这个,看看你得到了什么输出。我不确定在这里保存是正确的答案,这取决于您如何在控制器中创建@project。另外,请查看@Babur 下面所说的内容。

标签: ruby-on-rails associations


【解决方案1】:

你有

has_many :favorites, :through => :favorite_projects

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

has_many :favorites, :through => :favorite_projects, :source => :project

【讨论】:

  • 谢谢!我还有一个额外的错误,即“@project”没有在我的控制器中定义。我在我最喜欢的操作中添加了以下行:@project = Project.find(params[:id])
猜你喜欢
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多