5

I'm building a form in Rails3 and Formtastic. I have the following field:

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"} %>

Which generates HTML similar to:

<input id="post_one" name="post" type="radio" value="one" />Awesome</label>
<input id="post_two" name="post" type="radio" value="two" />Great</label>
<input id="post_three" name="post" type="radio" value="three" /> Nice</label>

That work flawlessly!

Now I'd like to know how I could pass in an option that would mark "Great" as the default (selected) value. I tried doing the following, but I can't get it to work.

<%= f.input :housing, :as => :radio, :collection => {"Awesome" => "one", "Great" => "two", "Nice" => "three"}, :default => "one" %>

I also tried passing in :selected and :checked instead of :default but alas, it does not work.

Does anybody know of a way to do this?

Thanks!


Edit: Aditya brings up a very good point. Some searching yielded this helpful tip.

Community
  • 1
  • 1
Yuval Karmi
  • 26,277
  • 39
  • 124
  • 175

3 Answers3

8

Set the HTML options on a specific radio input option with a 3rd element in the array for a collection member as follows:

<%= f.input :author, :as => :radio, :collection => [["Test", 'test'], ["Try", "try", {:checked => true}]]
dkniffin
  • 1,295
  • 16
  • 25
Ivan
  • 3,187
  • 1
  • 19
  • 16
6

Have you tried setting the value of the :housing attribute of your model to the default value? You could do that just before you start the form or in the controller or the best way is to do that in the model initialize? View may not be the best place to default IMHO.

Aditya Sanghi
  • 13,370
  • 2
  • 44
  • 50
  • thank you! while this doesn't really answer the original question, you do bring up a very good point. See link attached with my question. – Yuval Karmi Dec 27 '10 at 08:26
  • I also meant to imply that selected/default option is not really available on formtastic. The suggested route is to default the value in the model itself. Here's a link to the discussion about it's existence, history and removal. https://github.com/justinfrench/formtastic/wiki/Deprecation-of-:selected-option – Aditya Sanghi Dec 27 '10 at 10:44
  • 1
    But how to make this when there is no model associated with form? – barbacan Aug 22 '11 at 14:52
6

There is no longer an option to do this in the view, the correct way is to initialize the model with the correct value by default, or to put the model into that state in the controller, as described by Aditya.

Justin French
  • 2,867
  • 19
  • 14
  • You looked and responded! Thank you so much :) – Yuval Karmi Dec 27 '10 at 12:10
  • 1
    But how to make this when there is no model associated with form? as @barbacan also asked. – Mirko Sep 06 '11 at 21:47
  • If there is no model, you shouldn't really be using `form_for` or `semantic_form_for` in this case. There's trade-offs in everything. I would try to actually have a model. SearchLogic has an ActiveModel-like Search model, for example. It's totally doable and not anywhere near as much work as you think, and the upside goes far beyond this problem — you can have validations, etc. – Justin French Sep 07 '11 at 23:52
  • We're using a Rails Engine to provide the model/controller logic, and our different apps are just view templates with the engine dropped in... Modifying the model/controller isnt really an option for us since different apps might have different default values. I guess this means our only option is to set it via jquery or something? – daybreaker Nov 09 '11 at 23:16
  • Your other options is "don't use Formtastic for this form, or for this part of the form" :) – Justin French Jan 16 '12 at 21:47