All threads / How do I give Users the option of either selecting from a dropdown menu or inputting their own and adding it DB for other users
Ask A Question


You’re not receiving notifications from this thread.

How do I give Users the option of either selecting from a dropdown menu or inputting their own and adding it DB for other users

Omar Khedr asked in Rails

I apologize for the newbie question. But, I've been trying to create a special type of association and all my efforts have been failing. I would like to give users the ability to select their home cities. I've put down a few options from which they can choose, but if its not currently in the list - I would like a User to simply be able to add it "Don't see your City, add it". Then another User who signs up later can see the new city in his list (I know I might have to handle duplicate requests so any tips would be amazing). I've listed all my relevant code below. Thank you so much!

HomeCity Migration

class CreateHomecities < ActiveRecord::Migration[5.0]
  def change
    create_table :homecities do |t|
      t.string :Hometown


HomeCity Reference to Users

class AddHomecitiesRefToUsers < ActiveRecord::Migration[5.0]
  def change
    add_reference :users, :homecity, foreign_key: true


class User < ApplicationRecord
  cattr_accessor :current_user
  belongs_to :homecity, optional: true


class Homecity < ApplicationRecord
  has_many :users


<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
<%= devise_error_messages! %>
<div class="field">
  <%= f.label :homecity %>
  <%= f.collection_select :homecity_id, Homecity.all, :id, :Hometown %>
<div class="form-group">
  <%= f.submit "Update", class: 'btn btn-lg btn-block btn-primary' %>
<% end %>             


bigapple = Homecity.create!(Hometown:"New York City")
techhub = Homecity.create!(Hometown:"San Francisco")
longhorns = Homecity.create!(Hometown:"Austin")
angels = Homecity.create!(Hometown:"Los Angeles")
windycity = Homecity.create!(Hometown:"Chicago")

Homecities_controller.rb (Created new controller file based on research)

class HomecitiesController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
def new
 @homecity =
def create
 @homecity =
def homecity_params

Example similar to how Facebook does it with Education

This worked amazing thank you so much Jack for pointing me in the right direction :) In case anyone else looks at Chris' source code - it works amazing the one thing is depending on your text editor you might want to do this for the bootstrap button --> or else you'll get a editor error message (I use UltraEdit)

I wanted something like this, but for my purposes more complicated than it's worth. But glad to see the posting and video. Just realized all the posts are four years old. But seems it is well supported and used.

I wanted something like this

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 68,326+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

    © 2022 GoRails, LLC. All rights reserved.