嵌套用户资源的控制器

时间:2019-07-10 18:28:23

标签: ruby-on-rails reactjs axios

在我的应用中,我有usersboard_games。每个对象都有多个对象,因此我制作了一个称为user_board_games的:through表,可以在其中存储board_game_iduser_id,以便用户可以建立自己拥有的棋盘游戏库。

我有前往user_board_games的路线,如下所示:

Rails.application.routes.draw do
  mount_devise_token_auth_for "User", at: "api/auth"
  namespace :api do
    resources :board_games do
      resources :game_sessions
      resources :users
    end
    resources :users do
      resources :board_games
      resources :friends
      put '/get_board_games', to: 'board_games#get_board_games'

    end
  end

  get "*other", to: "static#index"
end

(看跌期权是一种可能的解决方案,我将在稍后提到)。

我的axios呼叫看起来像这样:

componentDidMount() {

    const userId = this.props.user.id 
    axios.get('/api/board_games')
      .then(res => {
        console.log(res.data)
        this.setState({games: res.data});
      })
    axios.get(`/api/users/${userId}/board_games/`)
      .then(res => {
        console.log(res.data); 
        this.setState({user_games: res.data});
      } )

  }

由于某些原因,这些调用都在我的board_games控制器中命中了我的index方法。 console.log提供的棋盘游戏数组与所有棋盘游戏都在用户库中相同,但是我没有在用户库中添加任何棋盘游戏,因此第二个数组应为空。

我的friends表也存在此问题,该表仅属于该用户,并保留其他用户的名称或ID的字符串。当我通过表单将朋友添加到一个用户,然后切换用户和axios.get朋友时,它的作用就像所有朋友都属于所有用户并在user2下显示user1的朋友。

当我检查sqlectron来查看user_board_games中没有什么时,什么都没有,因此它并不是将所有board_games实际添加到该表中,它只是对board_games进行了两次索引,而与路径无关。

昨晚,有人在帮助我,并建议在我的board_games控制器中创建另一个方法,该方法告诉它在获取user_id时应如何处理,以便在正确的表中进行发布和获取。

感谢您提供的任何帮助。让我知道是否需要查看更多代码以提供帮助。

编辑以添加请求的代码:

型号:

board_game.rb

class BoardGame < ApplicationRecord
  has_many :game_sessions, through: :game_session_games
  has_many :user_board_games, :dependent => :destroy
  has_many :users, through: :user_board_games
  has_many :rounds
end

user.rb

class User < ActiveRecord::Base
  has_many :user_board_games, :dependent => :destroy
  has_many :board_games, through: :user_board_games
  has_many :game_sessions
  has_many :friends
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  include DeviseTokenAuth::Concerns::User
end

user_board_game.rb

class UserBoardGame < ApplicationRecord
belongs_to :user 
belongs_to :board_game   
end

控制器:

board_games_controller.rb

class Api::BoardGamesController < ApplicationController
  before_action :set_board_game, except: [:index, :create]

  def index
    render json: BoardGame.all
  end

  def show
    render json: @board_game
  end

  def create
    board_game = BoardGame.new(board_game_params)
    if board_game.save
      render json: board_game
    else
      render json: board_game.errors
    end
  end

  def update
    if @board_game.update(board_game_params)
      render json: @board_game
    else
      render_error(@board_game)
    end
  end

  def get_board_games
    BoardGame.find_user_board_games(params[:user_id])
  end

  def destroy
    @board_game.destroy
  end

  private

  def set_board_game
    @board_game = BoardGame.find(params[:id])
  end

  def board_game_params
    params.require(:board_game).permit(
      :user_ids [],
      :title,
      :min_players,
      :max_players,
      :base_game,
      :time_needed,
      :company,
    )
  end
end

user_board_games_controller.rb

class Api::UserBoardGamesController < ApplicationController
  def index 
    render json: UserBoardGame.all 
  end 

  def destroy
    @user = User.find(params[:user_id])
    @user_board_game = @user.user_board_games.find(params[:board_game_id])
    @user_board_game.destroy
  end
end

相关耙路:

             api_board_games GET      /api/board_games(.:format)                                                               api/board_games#index
                             POST     /api/board_games(.:format)                                                               api/board_games#create
              api_board_game GET      /api/board_games/:id(.:format)                                                           api/board_games#show
                             PATCH    /api/board_games/:id(.:format)                                                           api/board_games#update
                             PUT      /api/board_games/:id(.:format)                                                           api/board_games#update
                             DELETE   /api/board_games/:id(.:format)                                                           api/board_games#destroy
        api_user_board_games GET      /api/users/:user_id/board_games(.:format)                                                api/board_games#index
                             POST     /api/users/:user_id/board_games(.:format)                                                api/board_games#create
         api_user_board_game GET      /api/users/:user_id/board_games/:id(.:format)                                            api/board_games#show
                             PATCH    /api/users/:user_id/board_games/:id(.:format)                                            api/board_games#update
                             PUT      /api/users/:user_id/board_games/:id(.:format)                                            api/board_games#update
                             DELETE   /api/users/:user_id/board_games/:id(.:format)                                            api/board_games#destroy
            api_user_friends GET      /api/users/:user_id/friends(.:format)                                                    api/friends#index
                             POST     /api/users/:user_id/friends(.:format)                                                    api/friends#create
             api_user_friend GET      /api/users/:user_id/friends/:id(.:format)                                                api/friends#show
                             PATCH    /api/users/:user_id/friends/:id(.:format)                                                api/friends#update
                             PUT      /api/users/:user_id/friends/:id(.:format)                                                api/friends#update
                             DELETE   /api/users/:user_id/friends/:id(.:format)                                                api/friends#destroy
    api_user_get_board_games PUT      /api/users/:user_id/get_board_games(.:format)                                            api/board_games#get_board_games
                   api_users GET      /api/users(.:format)                                                                     api/users#index
                             POST     /api/users(.:format)                                                                     api/users#create
                    api_user GET      /api/users/:id(.:format)                                                                 api/users#show
                             PATCH    /api/users/:id(.:format)                                                                 api/users#update
                             PUT      /api/users/:id(.:format)                                                                 api/users#update
                             DELETE   /api/users/:id(.:format)                                                                 api/users#destroy
                             GET      /*other(.:format)                                                                        static#index

0 个答案:

没有答案