Select receive custom value in Rails

Asked

Viewed 19 times

0

I own a select in my form:

<%= form.select :status, options_for_select([ ["Ativo", 0], ["Inativo", 1] ]), {include_blank: "Selecione ..."}, class: "form-control", required: true %>

To include a new record, my code works, only when I click the button to change the product, my select, does not receive the value of the database.

Example: Certain product, is with the value = 1, so when I click the change, it should be with the value = "Inactive".

1 answer

-1

It should automatically pick up the same model you’re using. You’re using Enum for that status column? Maybe that’s it.To solve the problem you can add the config selected

<%= form.select :status, 
      options_for_select(
        [ ["Ativo", 0], ["Inativo", 1] ],
        { selected: form.object.status }
      ), 
      {include_blank: "Selecione ..."}, 
      class: "form-control", 
      required: true %>

  • It worked, thank you very much!

  • What would it be like to use "Enum" for my column?

  • reading the question again, I figured out why I wasn’t magically picking up from the model. You can take the options_for_select and direct the status array. This options_for_select is not part of the object form, so I guess it didn’t pick up automatically. Can you validate? Could it be that you’re not even in doc from Rails

Browser other questions tagged

You are not signed in. Login or sign up in order to post.