Radio button tag checked conditon


#1

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


#2

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.