1

I've set in my model('Bambino') a multi-select field to assign a value to the string attribute 'status'. Find the code below from my form partial:

 <%= f.select(:status, options_for_select([['segnalato','segnalato'],
['inserito','inserito'],['drop','drop'],['concluso','concluso']])) %>

When I want to edit my record the edit form does not give me back the previous stored value but sets automatically the default value to 'segnalato' (E.g.:if I create a new record setting the status to 'inserito' and after I want to edit the record I get the edit form with the default value of 'segnalato' while I am expecting to see in the field 'inserito').

In this way when you edit a record chances to make a data entry mistake are very high. Why so? Is there a way to retrieve the proper 'status' value that was assigned when the record was created? Thanks

Sandro Palmieri
  • 1,193
  • 2
  • 12
  • 26

2 Answers2

0

Are you sure that @your_record.status is equal to one of those values? Check it out before any further debugging.

SpeedyWizard
  • 568
  • 6
  • 13
0

Whilst Andrey Deineko's answer is probably the one you want, there is a better way to achieve what you're doing: enum.

#app/models/bambino.rb
class Bambino < ActiveRecord::Base
   enum status: ['segnalato', 'inserito', 'drop', 'concluso']
end

This will give you the ability to use the following:

<%= f.select :color, Banbino.status.to_a.map { |w| [w.humanize, w] } %>

This will store a number for the status, whilst allowing you to define what each number means. It won't do anything about loading a pre-selected object (that's what Andrey's answer will do), but will give you the ability to make your application & select more succinct.

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147