显示个人资料名称而不是个人资料ID

时间:2016-11-26 21:09:55

标签: ruby-on-rails ruby forms ruby-on-rails-5

我有一个下拉选项链接到配置文件表中的名称。它们通过名为“profile_id”的键链接。当我选择这些选项并在显示页面中显示它时。它显示了profile_id,但我想显示名称。任何人都可以告诉我如何做到这一点。

表单视图选择如下所示:

<div class="field">
 <%= f.label :profile %><br>
 <%= f.collection_select :profile_id, Profile.all, :id, :name, {:prompt =>  'Please select an Aritst or Band'} %>
</div> 

Show View如下所示:

<p>
<strong>Artist:</strong>
<%= @album.profile_id %>
</p>

架构是这样的:

create_table "albums", force: :cascade do |t|
 t.string   "title"
 t.date     "released"
 t.string   "genre"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.integer  "profile_id"
 t.string   "image"
end

create_table "profiles", force: :cascade do |t|
 t.string   "name"
 t.date     "born"
 t.string   "bio"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
 t.string   "image"
end

表单确实为我提供了正确的名称选项,因此表格正确连接。但是当我查看结果时,它会给我个人资料ID,而不是我选择的名字。现在我知道这是因为我已经指定它来做这件事。但是如何让它显示名称。它可以在show view代码中完成,还是必须在控制器或模型中转换。

我打算将选定的选项也作为返回个人资料的链接。如果你也知道这一点,那将有助于我度过难关。

与往常一样,非常感谢所有帮助。

1 个答案:

答案 0 :(得分:3)

将您的节目动作更改为

<p>
<strong>Artist:</strong>
<%= @album.profile.name %>
</p>
相关问题