Question

How do I save information from my site http://yocoro.com/public into my database ? just by going to the page

PS: I'm using 2 gems geocoder and user-agent to optain this information.

//controller
class PublicController < ApplicationController  
  def index  
      @time = Time.now  
      @user_agent = request.env['HTTP_USER_AGENT']  
  end  
end

//view
<%= request.ip %>
<%= request.location.city %>  
<%= request.location.country_code %>  
<%= @user_agent %>
Was it helpful?

Solution

Based on input from @meager I was able to trim this way down.

Create a model and the appropriate table to go with it

rails generate model location ip:text city:text country:text user_agent:text

Then in your index action just put the info together and store it:

def index
  @ip = ip_param
  # Here is where you use the IP address and the geocoder gem to gather the 
  # info from the ip address. Then create the location object and save it.
  @location = Location.new
  @location.ip = @ip
  @location.city = city
  @location.country = country
  @location.user_agent = request.env['HTTP_USER_AGENT']
  @location.save
end

here is how I finally did it.. I try this before it didn't work but I finally understand why, my old code had "@location = location.new" the letter -l- was lower case and rails was giving me an error after I change to "@location = Location.new" bingo bango it worked!!!.

thank you so much to everyone.!!!

  @location = Location.new
  @location.ip = request.ip
  @location.city = request.location.city
  @location.country = request.location.country_code
  @location.user_agent = request.env['HTTP_USER_AGENT']

  if @location.save
    flash[:notice] = 'Location save'
  else
    flash[:notice] = 'Location did not save' 
  end

OTHER TIPS

At its most basic, Rails will let you map individual HTTP paths to a specific method in a specific controller:

get '/record_ip' => 'public#index'

This will match any URL starting with /record_ip, and use the parameters in that request within the method index in PublicController.

Inside index, you can do whatever you want with the database by accessing it via your models (of course, you can also do whatever you want that can be done via Ruby on your system.) When you're done, you need to use respond_to and render to decide what output to generate. If you don't call render explicitly, Rails will search for a template corresponding to the controller and method names, which is probably not what you want:

def index
  // Do something to the database
  // Your query parameters sent via a GET requests will be in the params hash

  respond_to do |format|
    format.json { render json: {'status' => "success"} }
  end
end

Now you can issue an HTTP GET request like http://yourserver.com/matchip?param1=val1. Note that Rails doesn't require you to have models - you could in fact do all your db manipulation with a SQL library.

Of course, that's just the simplest aspect of Rails routing and responses. There's a big iceberg beneath that tip for you to explore. If that's all you're doing, maybe Rails is overkill for you? You could manipulate a db pretty simply with just a custom PHP script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top