简体   繁体   中英

How do I use the zip code gem?

(Very new to Stack, please be gentle.)

I'm wondering how to incorporate a zip code function into my app. The app is a forum of a sort-of Reddit-like quality. As it stands, Users can post, comment, CRUD, upload pictures, all that sort of thing.

The goal is to somehow include a person's zip code on their posts. Trouble is, I've no idea how to do that. I downloaded the zip-codes gem , but I've no idea how to implement it.

Is there any idea how I can get help on this?

Thanks in advance.

The zipcodes gem just has city/state data for a given zipcode. If you want to get a user's location from their IP you'll want to look into something like geocoder which adds a location method to the request hash:

def create
  @post = post.new(post_params)
  @post.zipcode = request.location.postal_code

  if @post.save
    # ...
  else
    # ...
  end
end

For a lighter weight solution, check out freegeoip.net . You can send a request to a url like https://freegeoip.net/json/70.123.123.13 and grab the zipcode from the response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM