Forum: Ruby on Rails Edit is not working properly for radio_button

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Raji M. (Guest)
on 2008-11-19 06:50
Hi,

I m trying to edit contact info.. all other data other than radio button
are getting reflected in edit page .. But the same works fine while
creating new contact i.e it allows only one email id to be set as
primary but the same not working in edit.. it allows user 2 select  both
radio buttons
the view code is as follows
.
.
.
<table cellpadding="4" cellspacing="0" border="1" width="100%"
class="details">
<tr> <th colspan="4" align="left" bgcolor="#797ba8"><strong><font
color="fffff">Email Addresses</font></strong></th>
</tr> <tr> <% i= 0 %>
   <% for email in @contact.emails %>
   <% fields_for "contact[email_attributes][]", email do |e| %>
   <td nowrap class="formLabel">Email <%= i=i+1 %> </td>
   <td > <%= e.select ('email_type',%w{Business Personal},
:include_blank => false) %>
   <%= e.text_field :email,:size=>"35",:maxlength=>"80" %>
   <%= e.radio_button('isprimary', "1") %>
   </td> </tr>
<% end %> <% end %></table>
.
.
.
Xin yi L. (Guest)
on 2009-06-25 15:51
Hello,

I also have the same problem setting business categories as primary in
my edit_categories page using a radio_button.  The radio_button's
behavior changes, all buttons can be selected and can't be de-selected.
Everything works fine on my "new" page.  My codes is also like what Raji
had.  I added an onclick action just so user can de-select the
radio_button(it's now like a checkbox) and a note that user can just set
one category as primary.

Does anyone know a better way to solve this problem in a much cleaner
way? Please help.

I had this helper method which checks if the data already exist:

  def fields_for_business_category(category, &block)
    prefix = category.new_record? ? '' : 'existing_'
    fields_for("#{prefix}category_attributes[]", category, &block)
  end

This is my view:

<% category = business_category %>
<% fields_for_business_category(category) do |category_form| %>
<%= category_select_tag("category_id", category_form, {:class =>
'search'})%>
<%= category_form.radio_button("primary_category", "Y",
:onmousedown=>"this.chk = this.checked", :onclick=>"if (this.chk)
this.checked = false")%>
<%end%>

Thanks in advance..^_^
This topic is locked and can not be replied to.