gpt4 book ai didi

ruby-on-rails - 显示配置文件名称而不是配置文件 ID

转载 作者:数据小太阳 更新时间:2023-10-29 08:43:31 24 4
gpt4 key购买 nike

我有一个选项下拉列表,这些选项链接到配置文件表中的名称。它们通过名为“profile_id”的键链接。当我选择这些选项中的一个并将其显示在显示页面中时。它向我显示了 profile_id,但我想显示名称。谁能告诉我该怎么做。

表单 View 选择如下所示:

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

显示 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 而不是我选择的名称。现在我知道那是因为我指定了它来执行此操作。但是我如何让它显示名称。可以在显示 View 代码中完成还是必须在某个地方的 Controller 或模型中转换。

我打算将所选选项也作为返回配置文件的链接。如果你也知道这一点,那将对我有很大帮助。

一如既往,非常感谢所有帮助。

最佳答案

将您的表演 Action 更改为

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

关于ruby-on-rails - 显示配置文件名称而不是配置文件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40823183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com