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



我的目标是让用户添加从API宝石中提取的单个游戏(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

当我试图将游戏添加到我的库中时;Game(#70231217467720(预期,得到GiantBomb::Game,它是Class(#70231150447440(的实例";。所以我的@游戏是不正确的,但我不确定应该有什么。

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

class LibraryController < ApplicationController
#before_action :authenticate_user!
def index
@library_games = User.library_additions
end
end

我得到了"未定义的方法library_addments",即使它在模型中。如果我将User更改为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

我把我的库作为用户和游戏的连接表,但我认为我做得不对。这是我的模式:

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

我是错过了迁移,还是需要返工模型和控制器?

[编辑]这是我的路线,当我试图添加游戏时,我遇到了一个路径错误。

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

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

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

最新更新