Gainster
Gainster

Reputation: 5631

How to bind enum value to a field in rails

I have created a enum field in the database. Now i want to bind the field to the view in a such way that it shows all available values for selection. In this case Male and Female.

t.enum  :sex ,:limit => [:Male, :Female]

EDIT:

SQL generated:
Started PUT "/profiles/3" for 127.0.0.1 at 2012-06-04 22:11:35 -0700
Processing by ProfilesController#update as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"IJGsa4RfVeBC/LwD6PI69rJ5O0RxmPNTu7PavqK5hPM=", "profile"=>{"firstname"=>"Huzaifa ", "sex"=>"Female"}, "commit"=>"Update Profile", "id"=>"3"}
  [1m[36mProfile Load (1.0ms)[0m  [1mEXEC sp_executesql N'SELECT TOP (1) [profiles].* FROM [profiles] WHERE [profiles].[id] = @0', N'@0 int', @0 = 3[0m  [["id", "3"]]
  [1m[35mSQL (0.0ms)[0m  BEGIN TRANSACTION
  [1m[36mCACHE (0.0ms)[0m  [1mSELECT @@TRANCOUNT[0m
  [1m[35mSQL (0.0ms)[0m  COMMIT TRANSACTION
Redirected to http://localhost:3000/profiles/3
Completed 302 Found in 5ms (ActiveRecord: 2.0ms)        

Upvotes: 0

Views: 2490

Answers (2)

Gainster
Gainster

Reputation: 5631

Was not passing values,

 <%= f.select :sex , [[ "Male" ,0] , ["Female" , 3 ]] %>

Upvotes: 0

Gabriel Yarra
Gabriel Yarra

Reputation: 624

A range of values can be presented in a number of ways in a form, but it sounds like you want a selecte field. I'm assuming your column name is "enum"

= form_for @<model_name> |t| 
  = t.select :enum, [:Male, :Female]

Upvotes: 1

Related Questions