如何将枚举值绑定到rails中的字段

时间:2012-06-05 01:49:38

标签: ruby-on-rails ruby ruby-on-rails-3

我在数据库中创建了一个枚举字段。现在我想以这样的方式将字段绑定到视图,以便显示所有可用的值以供选择。在这种情况下男性和女性。

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

编辑:

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)        

2 个答案:

答案 0 :(得分:1)

一系列值可以在表单中以多种方式呈现,但听起来您想要一个选择字段。我假设您的列名是“enum”

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

答案 1 :(得分:0)

没有传递价值,

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