1

I am being super new to Rails, so, please keep it in mind reading my post.

I am trying to develop a very simple app where I can create a game and assign players to this game.

So far I have a scaffolded game and player resources. I have set has_many :players and has_many: games relationship, for both, - game and player models.

I added form.select in view form (new):

<%= form.label :player_id %>

<%= form.select :player_id, Player.all.collect{|x| [x.name]} %>

What I am trying to achieve is, - I want to be able to add multiple players to the single game. If I paste the same form.select field again it will overwrite first selection and eventually save only one (last) player.

I can, however, replicate this functonality in rails console. with:

g = Game.last
g.players << Player.find(1)
g.players << Player.find(2)

And it works.

Can you advise me on how to do same at FronEnd level? I understand if my approach is completely wrong, then let me know what will be a better one.

Many thanks in advance.

Update 1.

I have very default scaffold controller:

class GamesController < ApplicationController
  before_action :set_game, only: [:show, :edit, :update, :destroy]


  def index
    @games = Game.all
  end


  def show
  end

  def new
    @game = Game.new
  end

  def edit
  end

  def create
    @game = Game.new(game_params)

    respond_to do |format|
      if @game.save
        format.html { redirect_to @game, notice: 'Game was successfully created.' }
        format.json { render :show, status: :created, location: @game }
      else
        format.html { render :new }
        format.json { render json: @game.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @game.update(game_params)
        format.html { redirect_to @game, notice: 'Game was successfully updated.' }
        format.json { render :show, status: :ok, location: @game }
      else
        format.html { render :edit }
        format.json { render json: @game.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @game.destroy
    respond_to do |format|
      format.html { redirect_to games_url, notice: 'Game was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_game
      @game = Game.find(params[:id])
    end

    def game_params
      params.require(:game).permit(:name, :date, :player_id)
    end

end

and my db tables look like this:

class CreatePlayers < ActiveRecord::Migration[5.1]
  def change
    create_table :players do |t|
      t.string :name
      t.string :role
      t.integer :game_id

      t.timestamps
    end
  end
end

class CreateGames < ActiveRecord::Migration[5.1]

      def change
        create_table :games do |t|
          t.string :name
          t.date :date
          t.integer :player_id

          t.timestamps
        end
      end
    end 

Do i need to have action in game controller for it ?

fool-dev
  • 7,671
  • 9
  • 40
  • 54
BoYouLi
  • 11
  • 2

0 Answers0