Can the ActionView form input methods create <input> tags with inner html?
-
22-09-2019 - |
Question
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.
Solution
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.
OTHER TIPS
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.
<p><%= form.radio_button("ans", ans.num.to_s) %>
<%= form.label("Some Text") %></p>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow