NoMethodError (nil:NilClass 的未定义方法 'destroy'):即使它已定义



这是我目前正在处理的组件:

import React, { Component } from 'react';
import axios from 'axios'; 
import { connect } from 'react-redux';
import { Button, Card, Container } from 'semantic-ui-react'
class Games extends Component {
state = { games:[], user_games: [], showGames: false }
componentDidMount() {
const userId = this.props.user.id 
axios.get('/api/board_games')
.then(res => {
this.setState({games: res.data});
})
axios.get(`/api/users/${userId}/board_games`)
.then(res => {
console.log(res.data); 
this.setState({user_games: res.data});
} )
}
toggleGames = () => {
this.setState({ showGames: !this.state.showGames })
}
removeGame = (id) => {
const {userId} = this.props.user.id 
axios.delete(`/api/users/${userId}/board_games/${id}`)
.then(res => {
console.log(res);
console.log(res.data); 
})
}
addGame = (id) => {
const {userId} = this.props.user.id 
axios.post(`api/users/${userId}/board_games`, { userId, id })
.then(res => {
console.log(res);
})
}
userLibrary = () => {
const {user_games} = this.state 
return user_games.map( game => 
<Card key={game.id}>
<Card.Content>
<Card.Header>{game.title}</Card.Header>
<Card.Description>Players: {game.min_players} - {game.max_players}</Card.Description>
<Card.Description>Company: {game.company}</Card.Description>
<Card.Description>Time Needed: {game.time_needed}</Card.Description>
</Card.Content>
<Card.Content extra> 
<Button basic color='red' onClick={() => this.removeGame(game.id)}>
Remove from Library
</Button>
</Card.Content>
</Card> 
)
}
gamesList = () => {
const { games, user_games } = this.state 
return games.map( game =>
<Card key={game.id}>
<Card.Content>
<Card.Header>{game.title}</Card.Header>
<Card.Description>Players: {game.min_players} - {game.max_players}</Card.Description>
<Card.Description>Company: {game.company}</Card.Description>
<Card.Description>Time Needed: {game.time_needed}</Card.Description>
</Card.Content>
{ user_games.include ? (
<Card.Content extra>
<Button basic color='green' onClick={() => this.addGame(game.id)}>
Add to Library
</Button>
</Card.Content>
) 
: (
<Card.Content extra> 
<Button basic color='red' onClick={() => this.removeGame(game.id)}>
Remove from Library
</Button>
</Card.Content>
)  
}
</Card> 
)
}
render() {
const { showGames } = this.state 
return (
<Container>
<h1>Games</h1>
<h3>Your Games</h3> 
<Card.Group itemsPerRow={4}>{this.userLibrary()}</Card.Group>
{ showGames ? (
<div>
<Button basic onClick={this.toggleGames}>Done Adding</Button>
<Card.Group itemsPerRow={4}>{this.gamesList()}</Card.Group> 
</div>
)
: (
<Button basic onClick={this.toggleGames}>Add a Game</Button>
) 
}
</Container>
)
}
}
const mapStateToProps = state => {
return { user: state.user };
};
export default connect(mapStateToProps)(Games);

当我点击"从库中删除"时,我的服务器会给我:

NoMethodError(nil:NilClass的未定义方法"destroy"(:

app/controllers/api/board_games_controller.rb:30:在"销毁"中

控制台给我:

xhr.js:178删除http://localhost:3000/api/users/1/board_games/1500(内部服务器错误(

但我的控制器实际上定义了"销毁":

class Api::BoardGamesController < ApplicationController
# before_action :set_board_game
def index
render json: BoardGame.all
end
def show
render json: @board_games
end
def create
board_game = BoardGame.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

您会注意到before_action和set_board_game已被注释掉。当我取消对它们的评论时,一旦组件尝试挂载,我的axios.get就会失败。

我的axios.destroy做错了什么?

(总的来说,如果你看到其他可怕的错误(。

您的错误状态:

NoMethodError (undefined method `destroy' for nil:NilClass)

因为在您的destroy操作中:

def destroy 
@board_game.destroy 
end

您从未实例化过@board_game

正如您所注意到的,您注释掉了def set_board_game,它可能已经完成了任务。但就目前情况来看,@board_game为零。

您需要取消注释该位,并修复关于axious.get失败的其他注释。或者,直接在destroy方法中设置@board_game

但我的控制器实际上定义了"销毁">

错误与未定义destroy操作无关。如前所述,它指的是@board_game为零。

最新更新