Trying to use a radio button as a selection list

I am new using rails, I am trying to add to a user form a list of icon images and the user should select the icon he wants to have as icon. My problem is that I want to save the selected image in a field from user called "icon_id", on my user_controller I have de function where I find all icons @icons = Icon.find(:all) When I submit the form it doesn't save the selected radio button in that field. I hope someone can help me! Thanks in advance!!!

My code is: <%= stylesheet_link_tag 'top' %>        <%= javascript_include_tag :defaults %> <%= error_messages_for :user %> <% form_for :user, :url => users_path do |f| -%>

  <div class="boxform"> <div id="formulario"> <fieldset>   <p><label for="login"><%= _('Login') -%></label><br/> <%= f.text_field :login %></p>

<p><label for="email"><%= _('Email') -%></label><br/> <%= f.text_field :email %></p>

<p><label for="password"><%= _('Password') -%></label><br/> <%= f.password_field :password %></p>

<p><label for="password_confirmation"><%= _('Confirm Password') -%></

<br/>

<%= f.password_field :password_confirmation %></p>     <p> <label> <%= _('Select the icon you would like to have if you don not have a gravatar at gravatars.com:')%> </label></p>

<% for icon in @icons %>      <img class="imgicon" src="/images/top/<%=icon.image%>"/>      <input type="radio" id="icon_id" name="icon_id" value="<%=icon.id %>"

<%end%></fieldset>

</div> <div id="boton">     <button type="submit"><span><em> <%=_('Sign Up') -%> </em></span></

</div> </div> <% end -%>

My problem is that I want to save the selected image in a field from user called "icon_id", on my user_controller I have de function where I find all icons @icons = Icon.find(:all) When I submit the form it doesn't save the selected radio button in that field.

Whenever your controller doesn't respond to your parameters as expected, check your development log to see what parameters it received.

You'll probably find a bunch of params like this:

{ :user => { :login => 'foo', :email => 'bar' }, :icon_id => 61 }

Notice that icon_id is outside the user hash.

So, two problems:

<input type="radio" id="icon_id" name="icon_id" value="<%=icon.id %>"

First, you don't close the input tag.

Second, you don't nest icon_id within the user parameters in the name attribute of the input tag:

  <input type="radio" id="user_icon_id"                       name="user[icon_id]"                       value="<%=icon.id %>" />

Ciao, Sheldon.

Belén,

Thank you very much! I have done all your suggestions and Now it works !!!

I have another question, is there any way I could tell the radio button to check one option as default?? Thanks in advance Belen

Belen,

Thank you Brandon I have done it this way: <input type="radio" id="user_icon_id" name="user[icon_id]" value="< %=icon.id%>" <%= if icon.id==1 then "checked" end%> style="background- color:#fff; color: #0198ff;" />