# Radio button tag checked conditon

**URL:** <https://www.ruby-forum.com/t/radio-button-tag-checked-conditon/107412>\
**Category:** Rails\
**Created:** [June 21, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/radio-button-tag-checked-conditon/107412 "2007-06-21T15:57:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [June 21, 2007, 3:57pm UTC](https://www.ruby-forum.com/t/radio-button-tag-checked-conditon/107412/1 "2007-06-21T15:57:07Z")

</div>

Hello

Is it possible to pass a condition to radio\_button\_tag helper to check  
if smth is true or not?

\<%for brand in @brands%\>

\<%=radio\_button\_tag "product[brand\_id]", brand.id, :checked =\> true%\> \<%= brand.name %\>

\<%end%\>

I want to make it “checked” only for chosen brand in the database. I  
have done similar with check\_box\_tag on habtm relation with include? but  
here it does not seem to work

thx

---

<div class="post-metadata">

**Author:** ![pablo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@pablo](https://www.ruby-forum.com/u/pablo)\
**Post date:** [June 21, 2007, 7:08pm UTC](https://www.ruby-forum.com/t/radio-button-tag-checked-conditon/107412/2 "2007-06-21T19:08:56Z")

</div>

Pablo wrote:

> Hello
> 
> Is it possible to pass a condition to radio\_button\_tag helper to check  
> if smth is true or not?
> 
> \<%for brand in @brands%\>
> 
> \<%=radio\_button\_tag "product[brand\_id]", brand.id, :checked =\> true%\> \<%= brand.name %\>
> 
> \<%end%\>
> 
> I want to make it “checked” only for chosen brand in the database. I  
> have done similar with check\_box\_tag on habtm relation with include? but  
> here it does not seem to work
> 
> thx

I found a solution

First in a radio\_button\_tag

\<%for brand in @brands%\>

\<%=radio\_button\_tag "product[brand\_id]", brand.id, checked?(brand.name)%\> \<%= brand.name %\>

\<%end%\>

And then in a helper

def checked?(brand\_name)  
@product.brand.name == brand\_name rescue nil  
end

It seems to work! I put rescue nil for /new action as i am using the  
same form and @product does not exist. I hope this is the right  
solution.
