Title: Rails is Using HTML View When Requesting Javascript
Tags: ruby-on-rails;ruby-on-rails-3.2
Question: Weird issue where I'm asking for the JS file, but Rails is serving the HTML file. And it's only on my staging server (Heroku) and not on my local machine. 

I have a dynamic Javascript file which is something that needs to be included in other pages via a script to like so:

```<script type="text/javascript" src="http://example.com/embed.js&gt;</script&gt;
```

That embed maps to a controller and action which also handles HTML. The relevant route looks like this:

```match "/embed(.:format)" =&gt; "articles#embed", as: "embed"
```

And the controller action is pretty standard.

```def embed
  respond_to do |format|
    format.html do
      #it renders some HTML
    end
    format.js #no block is given
  end
end
```

And, I have two views under app/views/articles


embed.html.haml
embed.js.coffee


On my local machine, requesting localhost:3000/embed.js works. It renders the Javascript without a problem. However, on my staging server, here's what I see in the logs:

```Started GET "/embed.js" for 404.204.5303 at 2012-11-04 00:23:01 +0000
Processing by ArticlesController#embed as JS
  Rendered articles/embed.html.haml (1.5ms)
Completed 500 Internal Server Error in 2ms
```

The Internal Server Error is not the issue. The issue is that it recognises the request as JS, yet decides to render the HTML template and only on staging.

What's going on?

Comment: Thanks Rodrigo. While I can't figure the discrepancy between development and production, it appears the browsers request javascript as text/html and Rails seems to favour their requested type over the explicit format. In the controller, if I do `format.js { render action: "embed.js.coffee", content_type: "text/javascript" }` that seems to fix the problem on staging.

Comment: Except actually, now, the Coffeescript isn't getting compiled to Javascript so it's pretty useless. Will keep working on it…

Comment: I don't think this will solve the problem, but try using `format.json` instead of `format.js`. If nothing else works, you can make a new route (really, try everything before doing that) and force this route to use js, like `match "/embed(.:format)" => "articles#embed", as: "embed", :format => "js"`



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 3, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:11/04/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<ruby-on-rails><ruby-on-rails-3.2>', 'Title': 'Rails is Using HTML View When Requesting Javascript', 'ViewCount': 108, 'id': 13214948, 'user_AboutMe': "<p>I'm a developer, marketer and co-founder. </p>\n\n<p>I'm originally from the US, was educated in Canada (McGill Unversity) and received a Masters of Software Engineering and an MBA from the university of Melbourne.</p>\n", 'user_AccountId': 445050.0, 'user_DisplayName': 'Scott Arbeitman', 'user_Reputation': 311.0, 'user_Views': 39.0, 'user_id': 837249.0}}