Jack Gruber Jack Gruber - 10 months ago 81
Ruby Question

Display Profile Name Instead Of Profile ID

I have a drop down of options that links to the names in a profiles table. They are linked by a key named "profile_id". When I select on of these options and display it in the show page. It shows me the profile_id, but I want to show the name. Can anyone tell me how to do this.

The Form View Selection looks like this:

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


The Show View looks like this:

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


The schema is like this:

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


The form does give me the correct name options so the tables are linking up correctly. But when I view the results, it gives me the profile id and not the name I selected. Now I am aware that is because I have specified for it to do this. But how do I get it to show the name. Can it be done in the show view code or does it have to be converted in the controller or model somewhere.

I intend on that selected option also being a link back to the profile. If you know this also, that would help me out big time.

As always, all help is greatly appreciated.

Answer Source

Change your show action to

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