Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

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>
      </select>
    ```

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:

<?php
    if (isset($_POST)) { echo $_POST; }
?>

Perhaps there is some equivalent in RoR?

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