如何在用户个人资料页面上显示“收藏的帖子”?

时间:2016-11-23 03:04:48

标签: ruby-on-rails

我有最初的收藏夹和不喜欢的功能为用户工作,但是当我尝试在个人资料页面上显示所有收藏帖子的列表时,我在用户#Show中遇到了无方法错误。< / p>

User.rb中的错误:

undefined method `id' for nil:NilClass

  def favorite_for(post)
     favorites.where(post_id: post.id).first #This line
  end

  private

我不太确定我缺少什么,以显示收藏的帖子。我是否错过了生成迁移的问题AddUserToFavorites我试图挽回迁移但是也遇到了错误?

我的代码如下:

User.rb

class User < ApplicationRecord
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
  belongs_to :role
  before_validation :set_default_role
  has_many :posts, dependent: :destroy
  has_many :comments, dependent: :destroy
  has_many :votes, dependent: :destroy
  has_many :favorites, dependent: :destroy

  def favorite_for(post)
     favorites.where(post_id: post.id).first
  end

  private
    def set_default_role
      self.role ||= Role.find_by_name('registered')
    end
end

Favorites_controller.rb

class FavoritesController < ApplicationController
  before_action :require_sign_in

  def create
    post = Post.find(params[:post_id])
    favorite = current_user.favorites.build(post: post)

    if favorite.save
      flash[:notice] = "Saved as favorite!"
    else
      flash[:alert] = "Favorite failed to save."
    end
    redirect_to [post.topic, post]
  end

  def destroy
     post = Post.find(params[:post_id])
     favorite = current_user.favorites.find(params[:id])

     if favorite.destroy
       flash[:notice] = "Post unfavorited."
     else
       flash[:alert] = "Unfavoriting failed."
     end
       redirect_to [post.topic, post]
   end
end

Post.rb

class Post < ApplicationRecord
  belongs_to :topic
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_many :labelings, as: :labelable
  has_many :labels, through: :labelings
  has_many :votes, dependent: :destroy
  has_many :favorites, dependent: :destroy

  validates :title, length: { minimum: 5 }, presence: true
  validates :body, length: { minimum: 20 }, presence: true
  validates :topic, presence: true
  validates :user, presence: true
...
...
..

用户/ show.html.erb

<h2>Favorites</h2>
   <%= render partial: "favorites/favorite", locals: { post: @post } %>

   <h2>Posts</h2>
   <%= render @user.posts %>

   <h2>Comments</h2>
   <%= render @user.comments %>

模式

  create_table "comments", force: :cascade do |t|
    t.text     "body"
    t.integer  "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.index ["post_id"], name: "index_comments_on_post_id"
    t.index ["user_id"], name: "index_comments_on_user_id"
  end

  create_table "favorites", force: :cascade do |t|
    t.integer  "user_id"
    t.integer  "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["post_id"], name: "index_favorites_on_post_id"
    t.index ["user_id"], name: "index_favorites_on_user_id"
  end

  create_table "labelings", force: :cascade do |t|
    t.integer  "label_id"
    t.string   "labelable_type"
    t.integer  "labelable_id"
    t.datetime "created_at",     null: false
    t.datetime "updated_at",     null: false
    t.index ["label_id"], name: "index_labelings_on_label_id"
    t.index ["labelable_type", "labelable_id"], name: "index_labelings_on_labelable_type_and_labelable_id"
  end

  create_table "labels", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.string   "title"
    t.text     "body"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "topic_id"
    t.integer  "user_id"
    t.float    "rank"
    t.index ["topic_id"], name: "index_posts_on_topic_id"
    t.index ["user_id"], name: "index_posts_on_user_id"
  end

  create_table "roles", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "topics", force: :cascade do |t|
    t.string   "name"
    t.boolean  "public",      default: true
    t.text     "description"
    t.datetime "created_at",                 null: false
    t.datetime "updated_at",                 null: false
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "",    null: false
    t.string   "encrypted_password",     default: "",    null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,     null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "username"
    t.datetime "created_at",                             null: false
    t.datetime "updated_at",                             null: false
    t.integer  "role_id"
    t.boolean  "admin",                  default: false
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["role_id"], name: "index_users_on_role_id"
  end

  create_table "votes", force: :cascade do |t|
    t.integer  "value"
    t.integer  "user_id"
    t.integer  "post_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["post_id"], name: "index_votes_on_post_id"
    t.index ["user_id"], name: "index_votes_on_user_id"
  end

end

User_controller.rb

class UsersController < ApplicationController
  def show
      @user = User.find(params[:id])
  end
end

_form.html.erb

<% if favorite = current_user.favorite_for(post) %>
 <%= link_to [post, favorite], class: 'btn btn-danger', method: :delete do %>
   <i class="icon ion-ios-heart"> </i>&nbsp; Unfavorite
 <% end %>
<% else %>
 <%= link_to [post, Favorite.new], class: 'btn btn-primary', method: :post do %>
   <i class="icon ion-ios-heart-outline"> </i>&nbsp; Favorite
 <% end %>
<% end %>

如果需要更多代码,我会提供。感谢您的帮助,非常感谢!

0 个答案:

没有答案