# Edit is not working properly for radio\_button

**URL:** <https://www.ruby-forum.com/t/edit-is-not-working-properly-for-radio-button/153593>\
**Category:** Rails\
**Created:** [November 19, 2008, 5:50am UTC](https://www.ruby-forum.com/t/edit-is-not-working-properly-for-radio-button/153593 "2008-11-19T05:50:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [November 19, 2008, 5:50am UTC](https://www.ruby-forum.com/t/edit-is-not-working-properly-for-radio-button/153593/1 "2008-11-19T05:50:03Z")

</div>

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

 \<% i= 0 %\> \<% for email in @contact.emails %\> \<% fields\_for "contact[email\_attributes][]", email do |e| %\> \<% end %\> \<% end %\>

| **Email Addresses** |
| --- |
 | Email \<%= i=i+1 %\> | \<%= e.select ('email\_type',%w{Business Personal}, :include\_blank =\> false) %\> \<%= e.text\_field :email,:size=\>"35",:maxlength=\>"80" %\> \<%= e.radio\_button('isprimary', "1") %\> |

. . .

---

<div class="post-metadata">

**Author:** ![raji](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/eb8c5e/32.png) [@raji](https://www.ruby-forum.com/u/raji)\
**Post date:** [June 25, 2009, 1:51pm UTC](https://www.ruby-forum.com/t/edit-is-not-working-properly-for-radio-button/153593/2 "2009-06-25T13:51:12Z")

</div>

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…🙂
