在编辑或创建帖子时,我遇到了覆盖命名路由参数的问题,我收到错误undefined method playerId for nil:NilClass
。它仍然仅使用创建和编辑方法重定向到:id
而不是:playerId
参数。
下面,:playerId
应该是101,但是6是:id
,不确定为什么要提起它。
SELECT `players`.* FROM `players` WHERE `players`.`playerId` = 6 LIMIT 1 [["playerId", "6"]]
路线
resources :players, param: :playerId
控制器
def show
@player = Player.find_by(playerId: params[:playerId])
@season = PlayerStat.where("playerId = ?", @player.playerId).joins(:matches).where('matches.gameType = ?', 0).where('matches.teamId = ?', @player.teamId).group('year(matches.matchDate) DESC')
end
def edit
end
def create
@player = Player.new(player_params)
respond_to do |format|
if @player.save
format.html { redirect_to @player, notice: 'PLayer was successfully created.' }
format.json { render :show, status: :created, location: @player }
else
format.html { render :new }
format.json { render json: @player.errors, status: :unprocessable_entity }
end
end
end
def update
@player = Player.find params[:playerId]
respond_to do |format|
if @player.update(player_params)
format.html { redirect_to @player, notice: 'Player was successfully updated.' }
format.json { render :show, status: :ok, location: @player }
else
format.html { render :edit }
format.json { render json: @player.errors, status: :unprocessable_entity }
end
end
end
private
def set_player
@player = Player.find_by(playerId: params[:playerId])
end
def player_params
params.require(:player).permit(:playerId, :first_name, :last_name, :dob, :teamId, :jumper_no, :height, :weight, :image, team_attributes: [:teamId, :name], player_stats_attributes: [:playerId, :gameDate, :kicks, :marks])
end
答案 0 :(得分:1)
未定义的方法playerId for nil:NilClass
成功cat /proc/asound/card1/pcm0c/sub0/hw_params
access: MMAP_INTERLEAVED
format: S32_LE
subformat: STD
channels: 12
rate: 8000 (8000/1)
period_size: 400
buffer_size: 1200
或params[:layerId]
后问题为create
nil ,因为未传递任何update
1}}用于playerId
。因此redirect_to
为@player
,导致该错误。将代码更改为以下内容可以解决错误。
nil
同样适用于format.html { redirect_to player_path(@player.playerId), notice: 'PLayer was successfully created.' }
。
答案 1 :(得分:0)
您可以像这样定义完整路线:
put '/happy/node/:node_id', to: 'nodes#happy', as: :happy