ruby on rails - Rails4 - How to create and the best practices way to create list box for countries? -
i have user model name , email.
want add country listbox during user signup.
right can list users. want add country column model because want create view later users can search other users based on country.
i not using devise gem.
i know steps don't know correct way. should store countries in database ?
first add column
rails generate migration add_country_to_users country:<what_type_?>
then migrate
bundle exec rake db:migrate
then add validations in model
app/models/user.rb validates :country, inclusion: %w(full_list?)
add list box in registration form
app/views/users/new.html.erb <%= f.label :country %> <%= f.f.select :country %>
add list box in update form
app/views/users/edit.html.erb <%= f.label :country %> <%= f.f.select :country %>
add in strong params
app/controllers/users_controller.rb def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation, :country) end
kindly help. thanks
i think best way store in .yml file, , store country code in database specific user.
for example:
countries: us: united states ger: germany
and can load file in intializer,
config = hashwithindifferentaccess.new(yaml.load(file.read(path_to_file)))`
update
Comments
Post a Comment