在我将"program_id"链接到帐户表后,rails generate migration AddProgramIDToAccounts program_id:interger
并添加 <td><%= account.program_id %>
index.hrml.erb 文件中以显示结果。但是,我获得的是 ID 而不是我在 _forms.htlm.erb 文件中创建的下拉列表中的"程序名称"。
<div class="form-group">
<%= f.label :program %><br>
<%= f.collection_select :program_id, Program.all, :id, :program, {prompt: "Choose a Program"}, {class: "btn btn-default dropdown-toggle"} %>
</div>
我觉得我错误地创建了迁移,但我不确定。
导轨 4.1.8
Ruby 2.1.5P273 (2014-11-13 修订版 48405) [x86_64-Linux]
有了这个,你说(评论)
<%= f.collection_select
:program_id, #the parameter name
Program.all, #the list of objects to use in the select
:id, #the parameter value
:program, #the method you call on each object to get the text you want to display in the select
{prompt: "Choose a Program"}, {class: "btn btn-default dropdown-toggle"} %>
因此,选择将显示在每个程序对象上调用 .program 的结果。 这对我来说感觉不对,但你没有提供你的架构的任何细节,所以我不能肯定地说。
你需要写
account.program.name
在您的索引.html.erb 文件中,而不是account.program_id