LondonGuy
LondonGuy

Reputation: 11098

How make a drop down menu reflect what's stored in it's corresponding db column in ruby on rails?

How do I make a drop down menu reflect what's stored in it's corresponding column in a database?

I have a dropdown menu for gender selection and it updates the database fine but goes back to default option in select menu on refresh where as all my text fields are pulling db data fine.

<%= form_for @profile, :remote => true,  do |f| %>
Username: <%= @profile.user.username %><br />
URL: http://site.com/<%= @profile.user.username %><br />
First Name: <%= f.text_field :first_name,   %><br />
Last Name: <%= f.text_field :last_name,  %><br />
I am: <%= f.select :gender, options_for_select([['Select Gender', ''],['Male','m'],['Female','f']]) %><br />
<%= f.submit 'update' %><br />
<% end %>

Any clue what I'm missing? Kind regards

Here's my model:

class Profile < ActiveRecord::Base

  belongs_to :user

   attr_accessible :first_name, :last_name, :gender, :motd

  # Local Variables
  # Regex Variables
  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  alpha_regex = /^[a-zA-Z]*$/
  alpha_numeric_regix = /^[a-zA-Z0-9_]*$/


    #Form Validation
    validates :first_name,    :presence      => true,
                              :length        => { :minimum => 2, :maximum => 15 },
                              :format        => {
                                                  :with => alpha_regex,
                                                  :message => "Your first name must contain letters only"
                                                }

    validates :last_name,     :presence      => true,
                              :length        => { :minimum => 2, :maximum => 15 },
                              :format        => {  
                                                  :with => alpha_regex,
                                                  :message => "Your last name must contain letters only"
                                                }

    validates :gender,        :presence      => true,
                              :inclusion     => { 
                                                  :in => %w( m f ), :message => "Are you male or female?"
                                                }


end

Update method from controller

 def update

   respond_to do |format|

     if @profile.update_attributes(params[:profile])

       format.js   { render :js => "window.location = '#{settings_edit_profile_path}'" } 
       flash[:success] = "Profile updated" 
     else
      format.js   { render :form_errors }

    end
  end
end

Upvotes: 0

Views: 795

Answers (1)

josephrider
josephrider

Reputation: 943

options_for_select has a special syntax for selecting a value:

<%= f.select :gender, options_for_select([['Select Gender', ''],['Male','m'],['Female','f']], "#{@profile.gender}") %>

might work like you expect.

Or you could create a Gender model and use collection_select which does this by default:

<%= f.collection_select :gender, Gender.all, :value, :description, :prompt => true %>

Upvotes: 1

Related Questions