Extension - multiple checkboxes


#1

Ok, I have two extensions for my first radiant project.

Players

Name
Biography

Sports

Soccer
Football
Baseball
Hockey

In Players—>edit.html.erb

I want to generate a checkbox for each Sport (to assign players to
multiple sports).

I have no idea how to do this, but I have everything else working.


#2

Typically I’d do something like this inside the player editing form (in
Haml):

  • Sport.all.each do |sport|
    = check_box_tag ‘player[sport_ids][]’, sport.id,
    player.sports.include?(sport), :id => “player_sport_ids_#{sport.id}”
    %label{:for => “player_sport_ids_#{sport.id}”}= sport.name

Sean


#3

Sean C. wrote:

Typically I’d do something like this inside the player editing form (in
Haml):

  • Sport.all.each do |sport|
    = check_box_tag ‘player[sport_ids][]’, sport.id,
    player.sports.include?(sport), :id => “player_sport_ids_#{sport.id}”
    %label{:for => “player_sport_ids_#{sport.id}”}= sport.name

Sean

undefined method `sports’ for #<Player:

(Sorry, I’m new to rails! )

I would need to add has_and_belongs_to_many :sports to Player.rb model,
right?


#4

Sean C. wrote:

Typically I’d do something like this inside the player editing form (in
Haml):

  • Sport.all.each do |sport|
    = check_box_tag ‘player[sport_ids][]’, sport.id,
    player.sports.include?(sport), :id => “player_sport_ids_#{sport.id}”
    %label{:for => “player_sport_ids_#{sport.id}”}= sport.name

Sean

Okay, got all of this figured out and everything looks good in the
views.

I had to create a players_sports table with player_id and sport_id
columns and set has_and_belongs_to_many on player.rb model

The only problem is, it isn’t saving to the database on submit. Any tips
for making it work?

If I make an update method in player_controller, (which inherits
abstractmodelcontroller), do i also have to make new, edit, remove, etc?


#5

I figured this out, thank you for the initial help.