How to insert values from a Select field into the database

Asked

Viewed 637 times

2

I’m a beginner in Ruby on Rails and need to add a Select field to the user registration form, so I can add a "Type" for the user to be registered. I intend to populate the fields automatically while editing and viewing a user record.

I am implementing a system for a Graphic, which has the profiles "User", "Director" and "Graphic". My Select was structured as follows on _form user’s:

<div class="field">
  <%= f.label :tipo %><br>
  <%= f.select :tipo, ['usuario', 'grafica', 'diretor'] %>
</div>

Select values are not being entered in the bank! How should I proceed?

  • That column tipo really is tipo or is tipo_id? If it is tipo_id It needs to be changed there.

  • IS tipo even. tipo is an attribute of Usuarios.

  • Do you mind [Edit] the question including the controller code?

2 answers

3

If you are using Rails 4, have to look at the strog paramters, if you are allowing access to the attribute, follow example of the method:

def your_model_params
  params.require(:account).permit(:tipo)
end

If you are using Rails 3, make sure the attribute is accessible. An example of select:

<%= f.select :email_provider, options_for_select(%w[Provider1 Provider2]) %>

I hope I helped, a good start is Rails guide :)

2

From what I understand that’s what you need

f.select(:tipo, [['usuario', 'usuario'], ['grafica', 'grafica'], ['diretor', 'diretor'] })
  • It worked together with the modification of the Strong Parameters, thank you!

Browser other questions tagged

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