views:

151

answers:

2

I hope my question is understandable, else I'm glad to clarify.

We have this sport event coming up, and I have the pleasure to register the people. :)

Facts:

  • one can have a team of 5-10 persons.
  • Need to know the order in which the people start.
  • Need to know who the team leader is.

I did a form in the following style:

  1. o [____Write first name here___] <--- this is a textfield
  2. o [____Write second name here__]
  3. o [___________ ... ____________]
  4. ...

the "o" is a radio-button, used to pick the team leader.

I have a model Person and a model Team. Every team has a leader_id (which is one of the Person IDs). Furthermore every team :has_many persons.

In the controller I have

def create
   @team = Team.new(params[:team])
   @team.save #just assume there are no errors
end

In the view I have (for the radio-buttons):

<input id="team_leader_id_**????**"
       name="team[leader_id]"
       type="radio"
       value="**????**"
/>

<input id="team_leader_id_**????**"
       name="team[leader_id]"
       type="radio"
       value="**????**"
/>    
# etc.

My question: What should I put at **????** ?

I don't know the IDs of the persons yet, as they haven't been created. I have to put some meta-ID there, which ruby on rails recognizes and links everything correctly?

You don't have to read the rest (it describes the hack I'm using)

As said, at the moment I'm doing an ugly hack: first save the Team (without generating a leader), then fetch the same team from the db, get its people, and find the first person matching the value of

params[:team][:leader_id].

Finally saving this person's ID in the field of leader_id of the team.

But this code is inefficient, huge and buggy, that I suspect there is an easier way.

+2  A: 

The radios should have the same name i.e.

<input id="team_persons_attributes_0_leader_id"
       name="team[persons_attributes][leader_id]"
       type="radio"
       value="0"
/>
<input id="team_persons_attributes_1_leader_id"
       name="team[persons_attributes][leader_id]"
       type="radio"
       value="1"
/>
<input id="team_persons_attributes_2_leader_id"
       name="team[persons_attributes][leader_id]"
       type="radio"
       value="2"
/>

This way there can only be one selected (via the browsers internal radio button mechanism) and params[:team][:persons_attributes][:leader_id] will contain an index to params[:team][:persons] which will be the leader's name.

to obtain the id

team = Team.create(params[:team])
params[:team][:persons].each_with_index{|name, idx|
   person = Persons.create(:name => name)
   #assuming no errors
   team.leader_id = person.id if params[:team][:persons_attributes][:leader_id] == idx
}
Question Mark
thanks. I updated my post. Silly mistake, problem remains. :)
jacob
just to clarify: ruby on rails sets team.leader_id to whatever I specified in the "value" attribute. With your example this could for example be "1" if I choose the second one. But I really want to have the ID of the person in that field, and not 1.
jacob
the id can be obtained using my example by params[:team][:persons][params[:team][:persons_attributes][:leader_id]][:id] - i'll update my answer to explain in more detail.
Question Mark
nice, tanks :) Not a one line solution, but probably there is none.
jacob
+1  A: 

It seems as though you are submitting a number of users in one go with one of them flagged as the team leader. In this case you could construct something like:

<%= radio_button_tag "[team][leader]","[1]" %>
<%= radio_button_tag "[team][leader]","[2]" %>>

Then in the controller create the people (using nested attributes?), then simply look up the ID of the one flagged as the leader for the team record.

askegg
thanks for your answer.
jacob