seddka
seddka

Reputation: 105

Getting countries states and cities in rails

I have a user model with a nested model for user's informations, and i have already a working country field with country_select gem, but it doesn't offer states and cities .

After some research i found this gem ruby geocoder and as it says in the documentation :

In Any Rack-Based Framework Detect Location of HTTP Request Get current user's city and country (using IP address). A location method is added to the standard Rack::Request which returns a Geocoder::Result object:

# Rails controller or Sinatra app
city = request.location.city
country = request.location.country_code

Basically i want to get off using country_select gem and use ruby geocoder

I have two models :

models/user.rb

class User < ApplicationRecord
  extend FriendlyId
  friendly_id :username, use: :slugged
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_one_attached :avatar, :dependent => :destroy
  # User Information
  has_one :user_information, :dependent => :destroy
  accepts_nested_attributes_for :user_information, :allow_destroy => true

  def with_user_information
    build_user_information if user_information.nil?
    self
  end

  # Login with username or email
  attr_accessor :login       
  validates :username, uniqueness: true, presence: true

  def login
    @login || self.username || self.email
  end

  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if login = conditions.delete(:login)
      where(conditions.to_h).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
    elsif conditions.has_key?(:username) || conditions.has_key?(:email)
      where(conditions.to_h).first
    end
  end
end

and a nested model :

models/user_information.rb

class UserInformation < ApplicationRecord
  belongs_to :user

  has_one :gender, :dependent => :destroy
  accepts_nested_attributes_for :gender, :allow_destroy => true

  has_one :relationship, :dependent => :destroy
  accepts_nested_attributes_for :relationship, :allow_destroy => true

  def age
    now = Time.current
    dob = self.born_in
    now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
  end

  def country_name
    country = ISO3166::Country[country_code]
    country.translations[I18n.locale.to_s] || country.name
  end
end

this my devise controller

controllers/accounts_controller.rb

class AccountsController < Devise::RegistrationsController

  def update
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
    prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
    resource_updated = update_resource(resource, account_update_params)
    yield resource if block_given?
    if resource_updated
      set_flash_message_for_update(resource, prev_unconfirmed_email)
      bypass_sign_in resource, scope: resource_name if sign_in_after_change_password?
      session[:return_to] ||= request.referer
      redirect_to session.delete(:return_to)
    else
      clean_up_passwords resource
      set_minimum_password_length
      session[:return_to] ||= request.referer
      redirect_to session.delete(:return_to), alert: resource.errors.full_messages[0]
    end
  end

  def settings
    @user = current_user
    if @user
      render "devise/accounts/settings"
    else
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  def passwords
    @user = current_user
    if @user
      render "devise/accounts/passwords"
    else
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  def security
    @user = current_user
    if @user
      render "devise/accounts/security"
    else
      render file: 'public/404', status: 404, formats: [:html]
    end
  end

  protected

  def update_resource(resource, params)
    if params[:current_password].blank? && params[:password].blank? && params[:password_confirmation].blank? && params[:email].blank?
     resource.update_without_password(params.except(:current_password, :password, :password_confirmation, :email))
    else
      resource.update_with_password(params)
    end
  end
end

Upvotes: 0

Views: 1349

Answers (1)

Gustavo Toro
Gustavo Toro

Reputation: 450

If you want to replace country_select with geocoder, you just need to take this values in controller that you need.

class UsersController < ApplicationController
  ...
  def create
   @user = User.new(user_params)
   @user.user_information.country = country_name(request.location.country_code)
   @user.user_information.city = request.location.city
   @user.save
  end
end

If you're using Devise...

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
  def new
    super
  end

  def create
    # put above logic here
  end

  def update
    super
  end
end

You need to implement helper/model methods needed.

Upvotes: 0

Related Questions