在 index.html.erb 文件中显示字符串而不是整数
Displaying a string and not a integer on an index.html.erb file
在我将 "program_id" 链接到帐户 table、rails generate migration AddProgramIDToAccounts program_id:interger
并添加之后
<td><%= account.program_id %>
在 index.hrml.erb 文件中显示结果。但是,我得到的是 ID,而不是我在 _forms.htlm.erb 文件中创建的下拉列表中的 "program name"。
<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>
我觉得我错误地创建了迁移,但我不确定。
Rails 4.1.8
ruby 2.1.5p273(2014-11-13 修订版 48405)[x86_64-linux]
你需要写
account.program.name
在您的 index.html.erb 文件中 account.program_id
有了这个,你说(评论)
<%= 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"} %>
因此,select 将显示在每个 Program 对象上调用 .program 的结果。这对我来说感觉不对,但您没有提供架构的任何详细信息,所以我不能肯定地说。
在我将 "program_id" 链接到帐户 table、rails generate migration AddProgramIDToAccounts program_id:interger
并添加之后
<td><%= account.program_id %>
在 index.hrml.erb 文件中显示结果。但是,我得到的是 ID,而不是我在 _forms.htlm.erb 文件中创建的下拉列表中的 "program name"。
<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>
我觉得我错误地创建了迁移,但我不确定。
Rails 4.1.8
ruby 2.1.5p273(2014-11-13 修订版 48405)[x86_64-linux]
你需要写
account.program.name
在您的 index.html.erb 文件中 account.program_id
有了这个,你说(评论)
<%= 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"} %>
因此,select 将显示在每个 Program 对象上调用 .program 的结果。这对我来说感觉不对,但您没有提供架构的任何详细信息,所以我不能肯定地说。