I am very very new to Ruby on Rails, so sorry, if something is obviosly wrong or stupid :( But as a part of my university project I am supposed to create an app that uses RoR API. Everything was quite fine till this day. I have a model Game that has many nested Players inside. I am trying to build a POST request to create a new Player inside some Game. As "rails routes" says my path should be like "/api/v1/games/:game_id/players", in Postman I'm sending the following request to "http://localhost:3000/api/v1/games/HB236/players".
{
"player": {
"name": "GOBLIN BOBLIN",
}
}
However I get an error — NoMethodError in PlayersController#create undefined method `permit' for nil:NilClass. I am trying to understand — what am I doing wrong? Here are some of my files:
players_controller.rb
class PlayersController < ApplicationController
respond_to :json
before_action :get_game
def create
# @game = Game.find_by_code(params[:game_id])
@player = @game.players.create(params.permit(:name, :hp, :initiative, :languages, :perc, :inv, :ins, :armor, :conc))
render json: @player
# render json: @game.players
end
def index
# @game = Game.find_by_code(params[:game_id])
@players = @game.players
render json: @players
end
def get_game
@game = Game.find_by_code(params[:game_id])
end
# private
# def player_params
# params.require(:player).permit(:name, :hp, :initiative, :languages, :perc, :inv, :ins, :armor, :conc)
# end
end
player.rb
class Player < ApplicationRecord
belongs_to :game
end
poorly written routes.rb
Rails.application.routes.draw do
get 'welcome/index'
get 'effects/index'
get 'games/index'
post "", to: "welcome#redirect", as: :redirect
root 'welcome#index'
resources :games do
resources :players
resources :monsters
end
scope '/api/v1' do
resources :games, only: [:index, :show, :create, :update, :destroy] do
resources :players, only: [:index, :show, :create, :update, :destroy]
end
resources :effects, only: [:index, :show, :create, :update, :destroy]
resources :slug
resources :users, only: %i[index show]
end
scope :api, defaults: { format: :json } do
scope :v1 do
devise_for :users, defaults: { format: :json }, path: '', path_names: {
sign_in: 'login',
sign_out: 'logout',
registration: 'signup'
},
controllers: {
sessions: 'api/v1/users/sessions',
registrations: 'api/v1/users/registrations'
}
end
end
end
And also messages from the server:
app/controllers/players_controller.rb:7:in `create'
Started POST "/api/v1/games/HB236/players" for ::1 at 2023-02-25 02:40:28 +0300
Processing by PlayersController#create as */*
Parameters: {"game_id"=>"HB236"}
(0.1ms) SELECT sqlite_version(*)
↳ app/controllers/players_controller.rb:17:in `get_game'
Game Load (0.2ms) SELECT "games".* FROM "games" WHERE "games"."code" = ? LIMIT ? [["code", "HB236"], ["LIMIT", 1]]
↳ app/controllers/players_controller.rb:17:in `get_game'
Completed 400 Bad Request in 52ms (ActiveRecord: 1.6ms | Allocations: 8862)
ActionController::ParameterMissing (param is missing or the value is empty: player):
app/controllers/players_controller.rb:22:in `player_params'
app/controllers/players_controller.rb:7:in `create'
And also this is a response from GET request:
Started GET "/api/v1/games/HB236/players" for ::1 at 2023-02-25 02:51:52 +0300
Processing by PlayersController#index as */*
Parameters: {"game_id"=>"HB236"}
(0.2ms) SELECT sqlite_version(*)
↳ app/controllers/players_controller.rb:17:in `get_game'
Game Load (0.5ms) SELECT "games".* FROM "games" WHERE "games"."code" = ? LIMIT ? [["code", "HB236"], ["LIMIT", 1]]
↳ app/controllers/players_controller.rb:17:in `get_game'
Player Load (1.4ms) SELECT "players".* FROM "players" WHERE "players"."game_id" = ? [["game_id", 1]]
↳ app/controllers/players_controller.rb:14:in `index'
Completed 200 OK in 73ms (Views: 44.4ms | ActiveRecord: 4.0ms | Allocations: 11492)