# ActionDispatch host method

**URL:** <https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581>\
**Category:** rubyonrails-talk\
**Created:** [May 8, 2020, 3:54pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581 "2020-05-08T15:54:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tfantina](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/tfantina/32/1297_2.png) [@tfantina](https://discuss.rubyonrails.org/u/tfantina)\
**Post date:** [May 8, 2020, 3:54pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581/1 "2020-05-08T15:54:55Z")

</div>

I’m working on a large legacy Rails 3 project, in the `ApplicationController` there is a list of safe hosts and we have a method to see if the `request.host` is in that list. Locally I can call `request.host` and I get `localhost` back. However; in production, on Sentry there are some 200,000 errors: `undefined method 'host=' for #<ActionDispatch::Request:0x007ff28f2833e8>`.

I’m a little confused as to why this works locally but is apparently causing problems in production.

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [May 8, 2020, 6:38pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581/2 "2020-05-08T18:38:50Z")

</div>

Without looking at the rest of your code, it’s hard to say. But are you sure you are calling just `request.host` and not trying for a comparison `==` but only typing the one `=` (meaning assignment)? That’s something that has tripped me up from time to time over the past 20 years of Web programming. I’m embarrassed to say just how recently, in fact.

Walter

---

<div class="post-metadata">

**Author:** ![tfantina](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/tfantina/32/1297_2.png) [@tfantina](https://discuss.rubyonrails.org/u/tfantina)\
**Post date:** [May 8, 2020, 10:38pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581/3 "2020-05-08T22:38:46Z")

</div>

Ah thanks, I’m pretty infamous for assigning rather than comparing especially in tests but I’m afraid this is not the case in this instance. The method looks like this:

```ruby
    def block_unknown_hosts
       unless KNOWN_HOSTS.include?(request.host)
        request.host = ''
      end
    end

```

We get that `undefined method` error every few seconds in Sentry (which makes sense because this is a `before_filter` from the `ApplicationController`.

---

<div class="post-metadata">

**Author:** ![walterdavis](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/walterdavis/32/705_2.png) [@walterdavis](https://discuss.rubyonrails.org/u/walterdavis)\
**Post date:** [May 9, 2020, 4:00pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581/4 "2020-05-09T16:00:20Z")

</div>

That’s a puzzler, then. Are you using the same version of Rails in both places?

The only thing that error is saying is that the instance of request doesn’t have a “writer” method defined for host. It’s not a nil reference error, it’s a no method error. I would start by adding some logging around the assignment. See what instance of request you are getting, see what the methods available on that instance are, etc. You may also try using hash syntax to write this, like this:

request[:host] = ‘’

See if that allows you to write it out. If it’s not a symbol key, then try a string instead. I think that request in that context is a HashWithIndifferentAccess, so all three methods (dot syntax, hash with symbol keys, and hash with string keys) should all work equally. But I may be wrong in that assumption. That’s where I would start looking for answers.

Walter

---

<div class="post-metadata">

**Author:** ![tfantina](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/tfantina/32/1297_2.png) [@tfantina](https://discuss.rubyonrails.org/u/tfantina)\
**Post date:** [May 11, 2020, 6:43pm UTC](https://discuss.rubyonrails.org/t/actiondispatch-host-method/74581/5 "2020-05-11T18:43:43Z")

</div>

Thanks @walterdavis I think the issue was in attempting to assign the new host in the `unless` statement: `request.host = ''` apparently Rails doesn’t like that. I’m not sure if there is a way of setting the host in the application controller, if not I guess we’ll just have to raise an error in the event of a request from a bad host.
