我第一次在项目中使用collection_select并且用它来撞墙。
个人资料has_one团队,团队has_many个人资料。
在我编辑个人资料的视图中,我有这个。
<td><%= f.collection_select(:team_id, @team, :id, :title) %></td>
使用预期的团队标题填充下拉列表。我读过的几个例子似乎以非常类似的方式使用它。
我无法弄清楚何时保存配置文件,为什么它没有填充我的数据库中的team_id字段。在开发日志中,team_id正在通过。
Processing ProfilesController#update (for 127.0.0.1 at 2010-03-28 22:49:16) [PUT] Parameters: {"commit"=>"Update", "profile"=>{"dob(1i)"=>"2010", "second_name"=>"", "dob(2i)"=>"3", "role"=>"", "dob(3i)"=>"28", "project"=>"", "specialties"=>"", "about"=>"", "team_id"=>"1", "status"=>"", "first_name"=>""}, "authenticity_token"=>"sdTiFPGj9JCO3OEge5EGNGxZbQSsq9ME5LP342EBjyc=", "id"=>"3"}
更新控制器是标准的脚手架,这对我之前制作的配置文件模型的所有其他添加工作都很好。我错过了一些明显的东西吗?
编辑:用于更新操作的完整日志/ development.log
Processing UsersController#edit (for 127.0.0.1 at 2010-03-30 20:41:52) [GET] Parameters: {"id"=>"3"}
User Load (5.3ms) SELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1
Profile Load (2.0ms) SELECT * FROM "profiles" WHERE (user_id = 3) LIMIT 1
Team Load (6.9ms) SELECT * FROM "teams"
Rendering template within layouts/main
Rendering users/edit
Completed in 143ms (View: 53, DB: 14) | 200 OK [http://localhost/users/3/edit]
Processing ProfilesController#update (for 127.0.0.1 at 2010-03-30 20:42:00) [PUT] Parameters: {"commit"=>"Update", "profile"=>{"dob(1i)"=>"2010", "second_name"=>"", "dob(2i)"=>"3", "role"=>"", "dob(3i)"=>"28", "project"=>"", "specialties"=>"", "about"=>"", "team_id"=>"1", "status"=>"", "first_name"=>""}, "authenticity_token"=>"m6klUObst8uSATwkei5nY6ISx4OCHHCr9lDRYD5+mrc=", "id"=>"3"}
User Load (2.0ms) SELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1
Profile Load (2.2ms) SELECT * FROM "profiles" WHERE ("profiles"."id" = 3)
Profile Update (39.6ms) UPDATE "profiles" SET "updated_at" = '2010-03-30 19:42:00', "team_id" = 1 WHERE "id" = 3
[paperclip] Saving attachments.
Redirected to http://localhost:3000/users/3
Completed in 359ms (DB: 44) | 302 Found [http://localhost/profiles/3]
Processing UsersController#show (for 127.0.0.1 at 2010-03-30 20:42:00) [GET] Parameters: {"id"=>"3"}
User Load (1.5ms) SELECT * FROM "users" WHERE ("users"."id" = 3) LIMIT 1
Profile Load (2.2ms) SELECT * FROM "profiles" WHERE (user_id = '3') LIMIT 1
Rendering template within layouts/main
Rendering users/show
Team Load (1.0ms) SELECT * FROM "teams" WHERE ("teams"."id" = 1)
Completed in 115ms (View: 66, DB: 5) | 200 OK [http://localhost/users/3]
答案 0 :(得分:2)
试试这个:
<%= f.collection_select(:profile, :team_id, @team, :id, :title) %>