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| -%>

<%= _('Login') -%>
<%= f.text_field :login %>

<%= _('Email') -%>
<%= f.text_field :email %>

<%= _('Password') -%>
<%= f.password_field :password %>

<%= _('Confirm Password') -%>
<%= f.password_field :password_confirmation %>

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

<% for icon in @icons %>

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

<%end%>

<%=_('Sign Up') -%>
<% end -%>

Belén,

On Sat, May 31, 2008 at 5:02 AM, Belén [email protected] wrote:

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.

Sounds like you’re using restful_authentication, which uses
attr_accessible
to declare which attributes in the user model can be updated. If that’s
the
case, just add icon_id to the attr_protected declaration in your user
model.

Brandon


Sessions by Collective Idea: Ruby on Rails training in a vacation
setting
http://sessions.collectiveidea.com

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Saturday 31 May 2008 14:02:59 Belén wrote:

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:

Ciao,
Sheldon.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIQUhipGJX8XSgas0RAr82AKCln2WHw8wcesJp9UMD0tFKz/+aHACgiea/
hTKGM0OLcmqjCaPQwTCuHIs=
=pyX/
-----END PGP SIGNATURE-----

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,

On Sun, Jun 1, 2008 at 3:50 AM, Belén [email protected] wrote:

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

2 ways:

  1. set the value of the model field to the checkbox value
  2. <%= radio_button :model, :field, ‘val’, :checked => true %>

Brandon


Sessions by Collective Idea: Ruby on Rails training in a vacation
setting
http://sessions.collectiveidea.com

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;” />