LoadError(无法自动加载常量Board_Game,需要/models/board_game.rb对其进行定义)

时间:2018-11-26 17:58:57

标签: ruby-on-rails

完整服务器错误:

LoadError (Unable to autoload constant Board_Game, expected /Users/michellegarcia/coding_personal_projects/board_game_app/app/models/board_game.rb to define it):

app/controllers/api/board_games_controller.rb:3:in `index'

我在这里有一个拼写或大写错误,但找不到。我要console.log我已经手动植入到数据库中的所有Board_Games,以便列出它们。

控制器

class Api::BoardGamesController < ApplicationController

  def index
    render json: Board_Game.all
  end

  def show
    render json: @board_games
  end

  def create
    board_game = Board_Game.new 
    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 destroy 
    @board_game.destroy 
  end 

  private 

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

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

end

模型

class BoardGame < ApplicationRecord
  has_many :game_sessions, through: :game_session_games 
  has_many :rounds 
end

路线

Rails.application.routes.draw do
  mount_devise_token_auth_for 'User', at: 'api/auth'
  namespace :api do
    resources :board_games do 
      resources :game_sessions 
    end 
    #API ROUTES SHOULD GO HERE
  end

  #Do not place any routes below this one
  get '*other', to: 'static#index'
end

棋盘游戏组件

import React, { Component } from 'react';
import axios from 'axios'; 

//needs to do a call to the API to get the user's games from the user_board_game table 
//if the user has no games, it needs to default to "You have no games. Add games to your library by clicking
//"Add Game""
//don't know if "Add Game" should be its own component or a conditional render. 
//I want people to be able to add multiple games at once. Search Games. And have the option to add a game
//not on the list and auto-add it to their library. Also should have a checkbox for 
//"I have played this game before" so their backlog can be easily sorted. 
//if they've clicked on Add a Game, it should no longer render in the other component

class Games extends Component {

  state = { games:[] }
//wasn't hitting debugger in componentDidMount because I had Component capitalized. 
//then I was getting a 500 from my server because I didn't have resources :board_games in my routes.rb
//now getting 404
//this error "ActionController::RoutingError (uninitialized constant Api)" in server 
//created an api folder in my controllers folder and moved my proprietery controllers inside it
//new error: app/controllers/api/board_games_controller.rb:15: 
//syntax error, unexpected tINTEGER, expecting keyword_end
//got rid of 422 on line 15 
//new error: LoadError (Unable to autoload constant 
//Api::BoardGamesController, expected 
///Users/michellegarcia/coding_personal_projects/board_game_app/app/controllers/api/board_games_controller.rb
// to define it):
// my has_many :blank didn't have colons in board_game.rb 
  componentDidMount() {
//runs when the user wants to add a game from the database
//link to more info and button to add the game if it's not in their library already 
    axios.get('/api/board_games')
      .then(res => {
        // const games = res.data;
        console.log(res); 
        // this.setState({games});
      })
  }

  getUserGames = () => {
//runs when component mounts and then goes to the Games_List Function
//checks if the user has games user.games? return { the list} : return {<h1>You have no games</h1>}
  }

  gamesList = () => {
//gives each game with a link to more info  
  }

  render() {
    return (
      <div>
        <h1>Games</h1>
        <button>Add a Game</button> 
        <h3>Your Games</h3> 
      </div>
    )
  }
}

export default Games;

我将所有愚蠢的笔记和未定义的逻辑留在了应用程序中,以防您可以预见到问题的出现或有其他任何笔记。我只是来这里学习,所以我愿意对任何事情提出批评。

这里的大多数其他具有类似标题的帖子似乎在不同的命名空间中具有相同的类名问题,而我还没有。

2 个答案:

答案 0 :(得分:1)

您正在控制器中执行Board_Game.allBoard_Game.new,您的课程是BoardGame(没有_

答案 1 :(得分:0)

您可以检查控制器页面,并将文件名与class nameFile <ApplicationController进行比较。该名称必须相同。