Question

Im looking for the following thing: an array of all users (only 6 in this case) with a checkbox in front of their name, resulting in a list of selectable players for the game.

Current code:

<%= form_for @game, url: games_path, :method => "post" do |f| %>
    <%= f.label :name %>
    <%= f.text_field :name, :value => "#{current_user.name}\'s Game" %>
    <%= f.fields_for :participants do |ff| %>
        <%= ff.label :user_id %>
        <%= ff.text_field :user_id %>
        <%= ff.check_box :user_id %>
    <% end %>
    <%= f.submit "Create Game", class: "btn btn-primary" %>
<% end %>

I'm now having 3.times { @game.participants.build } in my controller which effectively gives me 3 textfields in which i can fill in the participant id in order to make a record in the table participants (which is linked to games).

I've been looking around for 1.5h now and i cant seem to find a proper answer. What i need is a syntax that gives me a list of all current users (say @users) with a checkbox attached to it. When I click the checkbox it should add its id to the parameters and i should be able to create a new game with the linked participant id's. However I'm getting some problems with the ID's attached to the check_box which always seems to be 1. I've read some stuff about checkboxes being a pain with hashes, but I have no other solution atm.

I tried:

<% @users.each do |i| %>
        <%= check_box_tag "alternate_numbers[#{i}]" %> <%= i.name %><br />
<% end %>

But i see no way to get that fixed up part of the form itself.

GamesController code (edit):

def new
    @users = User.paginate(page: params[:page])
    @games = current_user.games
    @game = Game.new
    3.times { @game.participants.build }
  end

def create
    @game = Game.new(params[:game])
    @newround = @game.rounds.new
    @newround.storyFragment = "New story!"
    if @game.save && @newround.save
      flash[:success] = "Created!"
      redirect_to game_path(@game.id)
    else
      redirect_to root_url
    end
  end

It's very vague to describe since im not exactly sure how to accomplish this.

In short: the check_box should contain the value of the user_id in the loop. I'm now filling in a manual ID with the text_field helper but i'd like to have the checkbox linked to the username that is right next to it in the view.

Any guidelines/solutions/tips?

Thx

Was it helpful?

Solution

Okay, so you're making a form for a new Game. You now have to feed that new Game, along with some Participants to your view.

def new
  @game = Game.new
  @participants = User.all # or the users you want
end

Now use those in your view. You were on the right track. Depending on how your create action works:

<% @participants.each do |p| %>
  <%= check_box_tag "participants[#{p.id}]" %> <%= p.name %>
<% end %>

I think what you were missing was the documentation for check_box_tag. The input attribute name is the argument.

You also seem to have a lot of logic in your controllers. Remember to keep the logic in the models, and only use the controllers to give the right objects to your views, and taking them for saving, for example. As the saying goes, "fat model, skinny controller".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top