Hi all, am writing a simple app that lets me pick a post type to go
under National or International.
the code bellow generates a drop-down from where I can pick the
options
<div>
<%= f.collection_select :postType_id, PostType.all(:order => "name
ASC"), :id, :name %>
</div>
what would be the syntax for this if instead I wanted to have two
radio buttons
I search for some sample code but all i've found is only with hard
coded strings.
any idea how I can generate two radio buttons for:
O National
O International
?
any help will be greatly appreciated.
oli.
I think you’re looking for something like radio_group. Based on your previous example, I would code it as:
<%= f.radio_group :postType_id, PostType.all(:order => “name ASC”).map {|pt| [ pt.id, pt.name ] }
Chris, thanks.
but iam getting an this error after trying your code:
undefined method `radio_group’ for #ActionView::Helpers::FormBuilder:0x1034d6800
am not sure if it is because this is a rails 3 app?
I googled this error but noting seemed helpful
<%= f.radio_group :postType_id, PostType.all(:order => “name ASC”).map {|pt| [ pt.id, pt.name ] }%>
any further help will be great.
oli.
Ah, yes. Sorry about that. The radio_group method isn’t a part of the FormBuilder, so while you can still call it within the scope of the form_for block, you can’t call it the way I told you to.
You will need to call it similar to:
<%= form_for @post do |f| %>
…
<%= radio_group "post[postType_id]", PostType.all(:order => "name ASC").map {|pt| [ [pt.id](http://pt.id/), [pt.name](http://pt.name/) ] }
…
<% end -%>
This should work if you adjust the code to your particular situation, but post back if not.
Ah, yes. Sorry about that. The radio_group method isn’t a part of the FormBuilder, so while you can still call it within the scope of the form_for block, you can’t call it the way I told you to.
So there really is a radio_group helper? There’s nothing about it in the api. Any links?
Er, yeah, that’s not going to work. It’s actually part of the Ruby standard lib as part of CGI::HtmlExtension, and now that I’m looking at it more closely, it’s not compatible with Rails use. My bad. Rails only supports the radio_button helper, as Jim pointed out.