显示配置文件名称而不是配置文件 ID
Display Profile Name Instead Of Profile ID
我有一个下拉选项 link 到配置文件 table 中的名称。它们由名为 "profile_id" 的键 link 编辑。当我 select 这些选项中的一个并将其显示在显示页面中时。它向我显示 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>
显示视图如下所示:
<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
该表格确实为我提供了正确的名称选项,因此 table 已 link 正确设置。但是当我查看结果时,它给了我个人资料 ID 而不是我 selected 的名字。现在我知道那是因为我指定了它来执行此操作。但是我如何让它显示名称。可以在显示视图代码中完成还是必须在某个地方的控制器或模型中转换。
我打算 selected 选项也作为 link 回到配置文件。如果你也知道这一点,那将对我有很大帮助。
一如既往,非常感谢所有帮助。
将您的表演动作更改为
<p>
<strong>Artist:</strong>
<%= @album.profile.name %>
</p>
我有一个下拉选项 link 到配置文件 table 中的名称。它们由名为 "profile_id" 的键 link 编辑。当我 select 这些选项中的一个并将其显示在显示页面中时。它向我显示 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>
显示视图如下所示:
<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
该表格确实为我提供了正确的名称选项,因此 table 已 link 正确设置。但是当我查看结果时,它给了我个人资料 ID 而不是我 selected 的名字。现在我知道那是因为我指定了它来执行此操作。但是我如何让它显示名称。可以在显示视图代码中完成还是必须在某个地方的控制器或模型中转换。
我打算 selected 选项也作为 link 回到配置文件。如果你也知道这一点,那将对我有很大帮助。
一如既往,非常感谢所有帮助。
将您的表演动作更改为
<p>
<strong>Artist:</strong>
<%= @album.profile.name %>
</p>