Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Saving request.remote_ip to the user database

geocoder remote_ip rails 3.2

  • Please log in to reply
6 replies to this topic

#1 rainless

rainless

    Passenger

  • Members
  • 4 posts

Posted 13 January 2014 - 01:28 AM

So finally got Geocoder to work with this code:

 

user.rb

geocoded_by :ip_address
before_validation :geocode

Only problem is that "ip_address" field.  I've tried to get the ip_address 90,000 different ways and NONE of them work.

 

I was finally able to capture the ip_address in the user.edit form by doing:

<%= f.text_field :ip_address, :value => request.remote_ip  %></div>

Now of course the problem is I want the ip address to automatically be saved in the user field whenever the user logs in or whenever a new user is created. to that end I have tried EVERYTHING and nothing has worked. I've tried:

 

users_controller.rb

  def ip_address
    ip_address = request.remote_ip
  end
  def ip_address
    @user.ip_address = request.remote_ip
  end    def edit
  def edit

  @user = User.find(params[:id])
    ip_address = request.remote_ip
  end
 
  def update
    @user = User.find(params[:id])
    ip_address = request.remote_ip
    if @user.update_attributes(params[:user])
      redirect_to @user
    else
      render :edit
    end
  end

NOTHING seems to save the request.remote_ip into the :ip_address field besides doing it in a form.

 

Ultimately what I want to do is get Geocoder to save the country in the :country field.

 

Any ideas?

 

 

 

 

 

 

 


#2 Jamie

Jamie

    Controller

  • Moderators
  • 114 posts
  • LocationThe UK

Posted 13 January 2014 - 12:06 PM

Hi
 
Welcome to Rails Forum.
 
You're trying to access the request object which isn't available inside a Model. Models exist outside the context of a web request.
 
When you are calling your Model method I'd send in the request.ip_address from the controller you use it.

 

Hope this helps!

 

Edit: Apologies I read this post when I was half asleep! I'll provide an example shortly.


Rails developer based in Newcastle, UK.
Web app owner - Twitter lover

#3 rainless

rainless

    Passenger

  • Members
  • 4 posts

Posted 13 January 2014 - 02:48 PM

Hi
 
Welcome to Rails Forum.
 
You're trying to access the request object which isn't available inside a Model. Models exist outside the context of a web request.
 
When you are calling your Model method I'd send in the request.ip_address from the controller you use it.

 

Hope this helps!

 

Thank you. Well actually... if you look above... I am calling from the users_controller (as marked "users_controller.rb")  

Can you maybe give an example of what else I should be doing?



#4 rainless

rainless

    Passenger

  • Members
  • 4 posts

Posted 13 January 2014 - 11:36 PM

Ok. I'll be anxiously awaiting. :)



#5 rainless

rainless

    Passenger

  • Members
  • 4 posts

Posted 15 January 2014 - 12:15 AM

Still waiting...



#6 João e o pé de feijão

João e o pé de feijão

    Passenger

  • Members
  • 5 posts

Posted 25 January 2014 - 08:09 PM

Just do it

class User < ActiveRecord::Base
   attr_accessible :ip_address
emd


#7 mark74

mark74

    Passenger

  • Members
  • 5 posts

Posted 05 February 2014 - 03:25 PM

Is there a way to get the real ip-address even if I am on the local machine?







Also tagged with one or more of these keywords: geocoder, remote_ip, rails 3.2

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users