0

I want a user (candidate) to select his skills (like python, rails, node..) while he is signing up.

I know I can use the following:

new.html.erb

<%= f.select(:skill, [['Python', 'Python'],
                                  ['Java', 'Java'],
                                  ['Rails', 'Rails'],
                                 ],
                                   { :multiple => true, :size => 5 }
                                 ) %>

BUT

What if I want the user to add a custom field?

Imagine the user has also the skill Javascript. As it is now, he cannot selected it.

How can I allow him to add a custom field in the f.select?

schema.rb

create_table "candidates", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "skill"
    t.index ["email"], name: "index_candidates_on_email", unique: true
    t.index ["reset_password_token"], name: "index_candidates_on_reset_password_token", unique: true
  end

I tried

<head>
  <script>$( "#user_organization_name" ).keypress(function() {
    $('#custom_org_id_select_menu').removeClass();
});</script>
</head>

<h2>Sign up for candidates</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :email,
                required: true,
                autofocus: true,
                placehoder: "name@gmail.com",
                input_html: { autocomplete: "email" }%>
    <%= f.input :password,
                required: true,
                hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length),
                input_html: { autocomplete: "new-password" },
                placeholder: "xYui6578Z!"%>
    <%= f.input :password_confirmation,
                required: true,
                input_html: { autocomplete: "new-password" },
                placeholder: "Repeat above password"%>
<div id="custom_org_id_select_menu">
        <%= f.select(:skill, [['Python', 'Python'],
                                  ['Java', 'Java'],
                                  ['Rails', 'Rails'],
                                 ],
                                   { :multiple => true, :size => 5 }
                                 ) %>
 </div>

  <div id="custom_org_id hide">
    <%= f.input :skill, label: "Others" %>
  </div>

It does work, but if the users writes something in other and selects something from the list (python, java etc), only what the user writes in other is sent via the params.

Prova12
  • 643
  • 1
  • 7
  • 25

1 Answers1

0

You can create a basic param for other skill and use it in the create action, like:

<div id="custom_org_id_select_menu">
  <%= f.select(:skill, [['Python', 'Python'], ['Java', 'Java'], ['Rails', 'Rails']],{ :multiple => true, :size => 5 }) %>
</div>

<div id="custom_org_id hide">
  <label>Others</label>
  <input name="other_skill" />
</div>
def create
  if params[:other_skill].present?
    # do something with the params[:other_skill] value
  end
  #...
end
Sovalina
  • 5,410
  • 4
  • 22
  • 39
  • Thanks. Being `candidate` created visa devise, I do not have a specific controller for it. Should I create a controller for `candidate`? – Prova12 Jul 27 '19 at 14:13
  • @Prova12 you can override the devise's registration controller (see example [here](https://stackoverflow.com/a/3553561/9541423)) – Sovalina Jul 27 '19 at 14:51