views:

41

answers:

3

I am generating an input like this:

<%= form.radio_button("ans", ans.num.to_s) %>

The generated html is:

<input id="myform_ans_1" name="myform[ans]" type="radio" value="1" />

But what I am looking for is this html:

<input id="myform_ans_1" name="myform[ans]" type="radio" value="1">Some Text</input>

Is there an option that I can pass into the radio_button method render the desired html?

As I understand the optional options hash that you can pass to radio_button, this only adds attributes to the html tag.

+1  A: 

You should add <label> tags with for attribute like this:

<input id="myform_ans_1" ... /><label for="myform_ans_1">Foobar</label>

And no, there is no parameter for that. You'll just have to use the label helper.

Eimantas
+1  A: 
<p><%= form.radio_button("ans", ans.num.to_s) %>

<%= form.label("Some Text") %></p>
Zepplock
This works, but I added `<%= form.label(:"ans_#{ans.num}", "Some Text") %>` to make the for attribute of the label be the same as the id for the input field.
lillq
+3  A: 

If your app will involve a lot of forms you should definitely take a look at formtastic. You can define your forms like so:

<% semantic_form_for @article do |form| %>

  <% form.inputs :name => "New Article" do %>
    <%= form.input :title %>
    <%= form.input :body %>
  <% end %>

  <% form.buttons do %>
    <%= form.commit_button %>
  <% end %>

<% end %>

This will generate all the markup required for html forms including fieldsets, legends, inputs and labels.

jonnii