agusgambina agusgambina - 3 months ago 10x
Ruby Question

Ruby on rails - bind a selected value to a select element for update

I can not figure out why this is not working, I saw a couple of examples and seems to be right.

I have two clases that are related, consultant and salary (a salary belongs_to a consultant).

The problem I have is, when I want to edit a salary, the consultant that appears on the form is not bind to the select (in the select it just appears the list as if I was creating a new one)

<%= :consultant_id, options_for_select({|s|[s.first_name,]}, params[:consultant_id]), {}, class: 'form-control' %>


I think you should check your route that the :consultant_id param is available on this page. Else you may need to change the params[:consultant_id] to something like salary.consultant_id

Alternatively, you can use the collection_select method as such:

f.collection_select(:consultant_id,, :id, :first_name, prompt: true, class: 'form-control')

Let me know whichever works for you.

Note: It's not best practice referring to domain object within your view.