将内容从一个控制器添加到另一个控制器的索引

Adding content from one controller to another controller's index

我的目标是让用户将从 API gem (https://github.com/games-directory/api-giantbomb) 中提取的个人游戏添加到他们的个人图书馆。我希望用户能够浏览其他人的图书馆。我通过搜索显示了游戏以及每个游戏的显示页面。

我 运行 遇到两个问题:无法将游戏添加到用户的库中,无法查看其他人的库。

这是我的游戏控制器:

class GamesController < ApplicationController

#search for games
  def index
  @games = GiantBomb::Search.new().query(params[:query]).resources('game').limit(100).fetch
  end

#Shows data for individual games
  def show
  @game = GiantBomb::Game.detail(params[:id])
  end

#Adding and removing games to a user's library
  def library
    type = params[:type]
    @game = GiantBomb::Game

    if type == "add"
      current_user.library_additions << @game
      redirect_to user_library_path, notice: "Game was added to your library"

    elsif type == "remove"
      current_user.library_additions.delete(@game)
      redirect_to root_path, notice: "Game was removed from your library"
    else
      # Type missing, nothing happens
      redirect_to game_path(@game), notice: "Looks like nothing happened. Try once more!"
    end
  end


private

  def game_params
    params.require(:game).permit(:name, :search, :query)
  end
end

当我尝试将游戏添加到我的库时,我得到“预期的游戏 (#70231217467720),得到 GiantBomb::Game,它是 Class(#70231150447440) 的一个实例”。所以我的@game 不正确,但我不确定应该用什么代替。

即使我可以将游戏添加到我的库中,我也无法查看其他用户的库。这是我当前的控制器。

class LibraryController < ApplicationController
  #before_action :authenticate_user!

  def index
    @library_games = User.library_additions
  end
end

我得到 'undefined method library_additions' 即使它在模型中。如果我将用户更改为 current_user 我可以看到该页面,但这意味着用户只能看到他们的页面而不能看到其他人。

这是我的游戏、用户和库模型:

class Game < ApplicationRecord
has_many :libraries
has_many :added_games, through: :libraries, source: :user
end
class User < ApplicationRecord

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  has_many :games
  has_many :libraries
  has_many :library_additions, through: :libraries, source: :game
end
class Library < ApplicationRecord
  belongs_to :game
  belongs_to :user
end

我为用户和游戏加入了我的图书馆 table,但我想我没有做对。这是我的架构:

ActiveRecord::Schema.define(version: 2020_11_19_143536) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "games", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.string "search"
  end

  create_table "libraries", force: :cascade do |t|
    t.integer "user_id"
    t.integer "game_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, 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.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: 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
  end

end

我是否缺少迁移或者我是否需要返工模型和控制器?

[edit] 这是我的路线,当我尝试添加游戏时出现路径错误。

Rails.application.routes.draw do
  devise_for :users
  resources :games do
    member do
      put "add", to: "games#library"
      put "remove", to: "games#library"
    end
  end
  resources :library, only:[:index]
  root to: 'pages#home'
  get '/search', to: 'games#search', as: :search
  get '/games', to: 'games#index', as: :index
  get '/user/:id', to: 'user#show'
  get '/user/:id/library', to: 'library#index', as: :user_library
end

此处,错误清楚地指出它需要一个 Game 而不是 GiantBomb::Game 的实例,因此您必须创建一个。

@game = Game.new(name: 'some name', other fields ....)

if type == "add"
  current_user.library_additions << @game

关于另一个错误,您只能在实例上调用关联方法,而不是在 class 本身上调用关联方法

def index
  # You could get the user'id through params for example
  @library_games = User.find(params[:user_id]).library_additions
end