dannymcc
dannymcc

Reputation: 3814

Rails 3, inserting local IP address into record field

I have a Rails 3 application and I would like to add a field to one of my models, the field will be called something along the lines of localip.

How do I add the current local IP to the field?

The standard create method in the controller is like this:

# POST /prescriptions
  # POST /prescriptions.json
  def create
    @prescription = Prescription.new(params[:prescription])

    respond_to do |format|
      if @prescription.save
        format.html { redirect_to @prescription, notice: 'Prescription was successfully created.' }
        format.json { render json: @prescription, status: :created, location: @prescription }
      else
        format.html { render action: "new" }
        format.json { render json: @prescription.errors, status: :unprocessable_entity }
      end
    end
  end

Is there some way of inserting it using the create method?

Upvotes: 0

Views: 670

Answers (1)

saihgala
saihgala

Reputation: 5774

I assume that by "current local IP" you mean IP of the client. If that's so then you can access this from env hash in your controller action -

current_local_ip = request.env['REMOTE_ADDR']

or

current_local_ip = request.env['REMOTE_HOST']

And if you mean server's ip by current local ip, just look up the value of HTTP_HOST key.

Upvotes: 2

Related Questions