Forum: Ruby on Rails radio button tag checked conditon

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.
734a7bce0ec28ecc63ab0bc92cfa3182?d=identicon&s=25 Pablo (Guest)
on 2007-06-21 15:57
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%>
<p><%=radio_button_tag "product[brand_id]", brand.id, :checked => true%>
<%= brand.name %>
</p>
<%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
734a7bce0ec28ecc63ab0bc92cfa3182?d=identicon&s=25 Pablo (Guest)
on 2007-06-21 19:08
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%>
> <p><%=radio_button_tag "product[brand_id]", brand.id, :checked => true%>
> <%= brand.name %>
> </p>
> <%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%>
<p><%=radio_button_tag "product[brand_id]", brand.id,
checked?(brand.name)%>
<%= brand.name %>
</p>
<%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.
This topic is locked and can not be replied to.