Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Create a dropdown using simpleform gem

Hi guys, I'm trying to create a dropdown with preset values and label but i'm not sure how to do this. My app is currently using the SimpleForms Gem.

Here is what i'm trying to achieve.

 <select id="font-style" name="signature[font]" class="form-control">
        <option value="Helvetica, Arial, sans-serif">Helvetica / Arial</option>
        <option value="Georgia, Times, serif">Georgia / Times</option>

3 Answers

Sean Perryman
Sean Perryman
13,810 Points

Though I am not a Ruby expert by any means, wouldn't you just use an f.input with the collections parameter?

<%= simple_form_for @i_dont_know_what_to_put_here do |f| %>
  <%= f.input :age, collection: { "Helvetica, Arial, sans-serif", "Helvetica, Arial, sans-serif" } %>
  <%= f.button :submit %>
<% end %>

Thanks this helped me get closer to what I needed.

  <%= f.input :font, collection: ["Helvetica Arial, sans-serif", "Georgia, Times, serif"] %>

But I still don't know how to set a name and value, i'de like the dropdown to show Serif and San-serif but when saved to save the values to the DB.

Sean Perryman
Sean Perryman
13,810 Points

Can you pass an array to the :label argument to compliment the collection argument?

<%= f.input :font, collection: ["Helvetica Arial, sans-serif", "Georgia, Times, serif"], label: ["Serif", "Sans-Serif"] %>

I think to figure out what data is getting passed you are going to have to echo out the contents of the post call. Again, new to RoR myself, but in PHP I would do this:

    if (isset($_POST)) { echo $_POST; }

Perhaps there is some equivalent in RoR?

Didn't Work the label is for the actual field label :-/