M.T Davis
M.T Davis

Reputation: 1098

Displaying a string and not a integer on an index.html.erb file

After I linked the "program_id" to the accounts table, rails generate migration AddProgramIDToAccounts program_id:interger and adding <td><%= account.program_id %> in the index.hrml.erb file to display the results. However, I'm getting the ID and not the "program name" like in the drop down I created in the _forms.htlm.erb file.

<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>

I'm feeling that I created the migration incorrectly but I'm not sure.

Rails 4.1.8

ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-linux]

Upvotes: 1

Views: 373

Answers (2)

Max Williams
Max Williams

Reputation: 32945

With this, you are saying (commented)

<%= 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"} %>

So, the select will display the results of calling .program on each Program object. That feels wrong to me but you don't provide any details of your schema so I can't say for sure.

Upvotes: 1

Shahzad Tariq
Shahzad Tariq

Reputation: 2857

You need to write

account.program.name

in your index.html.erb file instead account.program_id

Upvotes: 0

Related Questions