Ask A Question

Notifications

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

      t.timestamps
    end
  end
end

HomeCity Reference to Users

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

User.rb

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

Homecity.rb

class Homecity < ApplicationRecord
  has_many :users
end

Edit.html.erb

<%= 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 %>
  <br>
  <%= f.collection_select :homecity_id, Homecity.all, :id, :Hometown %>
</div>
<div class="form-group">
  <%= f.submit "Update", class: 'btn btn-lg btn-block btn-primary' %>
</div>
<% end %>             

Seeds.rb

Homecity.destroy_all
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 = Homecity.new
end
def create
 @homecity = Homecity.new(homecity_params)
end
private
def homecity_params
 params.require(:homecity).permit(:user_id)
end
end

Example similar to how Facebook does it with Education

Reply

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)
Close

Reply

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.

Reply

I wanted something like this

Reply
Join the discussion
Create an account Log in

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

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

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