Robert Ross
Robert Ross

Reputation: 1925

Rails 3.x dropdowns for models

So I'm runnning rails 3.1.0.rc6 and I'm having a hard time understanding drop downs (select elements) with models.

I have the following:

<%= form_for @user do |f| %>
      <div class="input">
        <%= f.label :email %>
        <%= f.text_field :email %>
      </div>

      <div class="input">
        <%= f.label :password %>
        <%= f.password_field :password %>
      </div>

      <div class="input">
        <%= f.label :password_confirmation, "Repeat Password" %>
        <%= f.password_field :password_confirmation %>
      </div>

      <div class="input">
        <%= f.label :first_name %>
        <%= f.password_field :first_name %>
      </div>

      <div class="input">
        <%= f.label :last_name %>
        <%= f.password_field :last_name %>
      </div>

      <div class="input">
        <%= f.label :birthday %>
        <%= date_select "user", "birthday" %>
      </div>

      <div class="input">
        <%= f.label :gender %>
        <%= select_tag :gender, options_for_select([['Male', 'male'], ['Female', 'female']]) %>
      </div>

      <div class="submit">
        <%= f.submit nil, :class => ['button', 'button_blue'] %>
      </div>
    <% end %>

Now I don't understand how to have an easy to use birthday select and gender select. The HTML it generates is not exactly friendly to use with model saves.

Any help would be great! Thanks.

Goal: Just be able to do an easy User.create with the posted data.

Upvotes: 1

Views: 286

Answers (3)

Michael Durrant
Michael Durrant

Reputation: 96604

Strongly recommend you use simple_form (based on formtastic). It's used by tens of thousands of developers daily has 45 forks, etc and is a standard with most organization I work with. You will never look back it is so simple (yet flexible). Written by Jose Valim who's written books on rails and is one of the leaders in the community.

https://github.com/plataformatec/simple_form

small excerpt (to address your specific questions about drop downs (select elements) with models:-

...

Now we have the user form:
<%= simple_form_for @user do |f| %>
  <%= f.input :name %>
  <%= f.association :company %>
  <%= f.association :roles %>
  <%= f.button :submit %>
<% end %>

Simple enough right? This is going to render a :select input for choosing the :company, and another :select input with :multiple option for the :roles. You can of course change it, to use radios and check boxes as well:
f.association :company, :as => :radio
f.association :roles,   :as => :check_boxes

...

You can still use a date_calendar gem for a nice calendar pop-up date picker and this Does anyone know any gems/plugins/tutorials related to exporting events to iCal, Google Calendar, Outlook from a Rails application? may also help

Upvotes: 1

Andrea
Andrea

Reputation: 420

For your date select, you could use jquery datepicker, which is very friendly to use.

First of all, if you write your form field like this

<div class="field">
    <%= f.label :birthday %><br />
    <%= f.text_field :birthday %>
</div>

it will automatically be given the id='user_birthday' (NB: take care to set it as text field). Then just add a simple configuration in javascript using jquery:

$(document).ready(function() {

    $.datepicker.setDefaults({
        dateFormat: 'dd/mm/yy',
        firstDay: 1,
        showOn: 'focus'
    }); 

    $('#user_birthday').datepicker();

});

Upvotes: 0

user483040
user483040

Reputation:

You can use the select form builder helper directly off your 'f' variable as such

f.select( :gender, options_for_select([["Male", "male"],["Female","female"]]))

This will make sure that the name and id for the select is generated in such a manner that your create will pick it up automatically.

Upvotes: 1

Related Questions