【问题标题】:Adding content from one controller to another controller's index将内容从一个控制器添加到另一个控制器的索引
【发布时间】:2021-03-03 18:18:09
【问题描述】:

我的目标是让用户将从 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

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

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

class LibraryController < ApplicationController
  #before_action :authenticate_user!

  def index
    @library_games = User.library_additions
  end
end

我得到“未定义的方法 library_additions”,即使它在模型中。如果我将 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

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

[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

【问题讨论】:

    标签: ruby-on-rails api model-view-controller


    【解决方案1】:

    这里,错误清楚地表明它需要一个 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 1970-01-01
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多