Mini John
Mini John

Reputation: 7941

Input Selection (Not Telling/Male/Female) in Rails4

Sorry if this is a too easy question but it's bugging me.

I need to create a simple selection field for

and the default when not entered to be "Not "Telling".

Do i just run a migration like:

rails g migration AddGenderToListings gender:select

and then in my Profile Edit Form

<%= f.select :gender, [["Not Telling", 1], ["Male", 2], ["Female", 3]] %>

But how do i display it afterwards on a show page ??

if just <%= @user.gender %> it puts the number of the selection.

Any enlightenment ?

Upvotes: 1

Views: 2549

Answers (2)

jfalkson
jfalkson

Reputation: 3739

In your model you can add:

GENDER_TYPES = ["Not telling","Male", "Female"]

Then in your form:

<%= f.select :gender,  User::GENDER_TYPES %>

The above is a simple fix that worked for me.

Upvotes: 2

Santhosh
Santhosh

Reputation: 29174

Add a new migration to set default value

Or rollback your migration, change it to

add_column :listings, :gender, :integer, :default => 1

And migrate again

EDIT In model you can add

def gender_txt
  ["Not Telling", "Male", "Female"][self.gender - 1]
end

In your views, you can display using

@user1.gender_txt
@user2.gender_txt

Upvotes: 2

Related Questions