因此,我有一个拥有许多用户的应用程序,它们具有用户配置文件。我希望能够收藏用户个人资料,该个人资料会添加到我的收藏夹标签中。我写了下面的代码,但是我收到了这个错误:
ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "favorites_users" does not exist
2014-02-04T02:18:56.217171+00:00 app[web.1]: LINE 5: WHERE a.attrelid = '"favorites_users"'::regcl...
^
现在我有以下代码:
favorite.rb
class Favorite < ActiveRecord::Base
has_and_belongs_to_many :users
end
favorites_controller.rb
class FavoritesController < ApplicationController
before_filter :find_user
def index
@favorites = @user.favorites
end
def create
@user = User.find params[:id]
@user.favorites << @user
def
def destroy
@favorite = @user.favorites.find_by_user_id params[:id]
@favorite.destroy unless @favorite.blank?
end
end
private
def find_user
@user = current_user
end
end
favorites.html.erb
<div class="row">
<%= render 'layouts/left_side' %>
<div role="main" class="col-md-6">
<div class="visitors-m">
<div class="col-md-12 showing">
<div class="row">
<div class="col-md-12">
<h4><%= pluralize(@favorites.size, 'favorites') %></h4>
<div class="col-md-3 row fr">
<select class="form-control">
<option value="">SORT BY</option>
<option value="1">General</option>
<option value="2">News</option>
<option value="3">Media</option>
<option value="4">Funny</option>
</select>
</div>
</div>
</div>
</div>
<%#= @favorites.map(&:name).to_sentence %>
<% @favorites.each do |favorite| %>
<div class="col-md-12 users_info visitors">
<div class="row">
<div class="col-md-12">
<div class="img-thumbnail">
<% if favorite.profile_image.present? %>
<%= image_tag(favorite.profile_image_url(:thumb), size: '100x100') %>
<% else %>
<%= image_tag("people_image.jpg") %>
<% end %>
</div>
<div class="users_about_info">
<h4><%= visitor.name %></h4>
<p class="visit-time">
<%= timeago_tag(current_user.visits.where(favorite: favorite).first.visited_at) %>
</p>
</div>
<div class="fr">
<p><%= favorite.address %></p>
<p><%= favorite.age %> years old</p>
</div>
<div class="clr"></div>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render 'users/visitors_filter' %>
</div>
收藏迁移
class CreateFavorites < ActiveRecord::Migration
def change
create_table :favorites do |t|
t.timestamps
end
end
end
答案 0 :(得分:1)
favorites_users不是收藏夹或用户下的列,当然您会收到该错误。你需要打电话:
User.find(1).favourites
或
Favourite.find(1).users
你最喜欢的实现是假设你只有一个最喜欢的(对喜欢这个词的语法用法。遗憾的是,它没有被广泛使用(大多数网站允许超过1个受欢迎的项目,或者在你的情况下,用户。)用户可以收藏超过1个用户个人资料,然后您需要最喜欢的关系。
如果您需要帮助,请与我联系。
模型
class User <ActiveRecord::Base
has_many :favourite_relationships
has_many :favourites, through: :favourite_relationships
has_many :people_who_favourited_me, through: :favourite_relationships, foreign_key: "favourite_id"
end
class FavouriteRelationships <ActiveRecord::Base
# attributes are ( user_id and favourite_id )
belongs_to :favourite
belongs_to :user
end
class Favourite < ActiveRecord::Base
has_many :favourite_relationships
has_many :users, through: :favourite_relationships
end
请将用户ID作为当前用户ID和收藏ID作为创建收藏夹的参数传递。
<% form_for Favourite.new do |f| %>
<%= f.hidden_field :user_id, value: current_user.id %>
etc....
并使用该表单创建收藏夹。