Skip to content

Commit

Permalink
Fix contact form data
Browse files Browse the repository at this point in the history
We also use the contact form to report various errors on the website.
To facilitate communication and better understand the problem,
we collect some basic information about user's client (browser).

Migrating to Fly introduced a bit of confusion with the IP address.
In most hostings we can get it using:

```ruby
request.remote_ip
request.remote_addr
```

or

```ruby
request.env['HTTP_X_REAL_IP']
request.env['REMOTE_ADDR']
```

However, research showed me that Fly has a custom field in the header.
For posterity, I have kept the most important links: [1] [2] [3] [4]

So the "Fly-Client-IP" field can be found in Rails under
`request.env['HTTP_FLY_CLIENT_IP']`, which doesn't seem intuitive to me.

[1]: https://fly.io/docs/reference/runtime-environment/#request-headers
[2]: https://community.fly.io/t/how-to-retreive-the-real-ip-address-of-an-http-request-with-v2-app-with-http-service-configuration/12095
[3]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Forwarded-For
[4]: https://stackoverflow.com/questions/10997005/whats-the-difference-between-request-remote-ip-and-request-ip-in-rails
  • Loading branch information
torrocus committed Jan 18, 2024
1 parent 8d97e51 commit 1063cf4
Showing 1 changed file with 1 addition and 0 deletions.
1 change: 1 addition & 0 deletions app/controllers/contact_forms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def new
end

def create
request.remote_ip = request.env['HTTP_FLY_CLIENT_IP'] || request.remote_ip
@contact_form = ContactForm.new(params[:contact_form])
@contact_form.request = request
@contact_form.deliver ? flash[:notice] = t('.success') : flash[:alert] = t('.fail')
Expand Down

0 comments on commit 1063cf4

Please sign in to comment.