# RJS not rendering correctly when mime-type is 'iphone'

**URL:** <https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688>\
**Category:** Rails\
**Created:** [April 4, 2009, 1:01am UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688 "2009-04-04T01:01:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 4, 2009, 1:01am UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/1 "2009-04-04T01:01:49Z")

</div>

I have an iPhone version of my site, and for some reason none of the  
rjs works when viewing the iPhone version. This happens instead:

ActionView::MissingTemplate (Missing template controller\_name/  
update.erb in view path app/views)

The rjs works fine when viewing the site normally, and it worked when  
viewing the iPhone version prior to upgrading Rails to 2.3.2.

Additional info: I have ‘Mime::Type.register\_alias “text/  
html”, :iphone’ in mime\_types.rb and the respond\_to block in the  
action is what’d you expect –

respond\_to do |format|  
format.js  
end

What could be the problem? Thanks.

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 17, 2009, 8:05am UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/2 "2009-04-17T08:05:48Z")

</div>

No one knows why this is happening? I created a new rails app from  
scratch, first uncommenting “Mime::Type.register\_alias “text/  
html”, :iphone” in mime\_types.rb. Then I put this in my application  
controller:

* * *

before\_filter :adjust\_for\_iphone

## def adjust\_for\_iphone request.format = :iphone if request.env[‘HTTP\_USER\_AGENT’][/(Mobile /.+Safari)/] end

I created an index controller, with an index and test action  
(index.html.erb, index.iphone.erb, test.js.rjs), and put a remote link  
in the index action to call test. Without any respond\_to block in the  
test action, I get this:

* * *

## ActionView::MissingTemplate (Missing template index/test.erb in view path app/views)

When this block is added to the test action:

* * *

## respond\_to do |format| format.js format.iphone { render ‘test.js.rjs’ } # also tried without this render block end

There are no errors whatsoever, however the rjs doesn’t get executed  
at all, even with Firebug showing the js in the response. Surely  
someone can help with this?

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 17, 2009, 6:13pm UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/3 "2009-04-17T18:13:59Z")

</div>

Andrew wrote:  
[…]

> There are no errors whatsoever, however the rjs doesn’t get executed  
> at all, even with Firebug showing the js in the response. Surely  
> someone can help with this?

The problem would appear to be that you’ve got your iphone mime-type  
defined as text/html. This means that even though Rails is correctly  
processing the RJS file, it is telling the browser that the file is  
HTML, not JavaScript. You’ll need a separate type (iphone\_js or  
something) defined as text/javascript.

RJS is evil anyway. 🙂

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 20, 2009, 12:25am UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/4 "2009-04-20T00:25:04Z")

</div>

Andrew wrote:

> Thanks for the advice; it worked fine pre-2.3 though.

I’m not sure why this would ever have worked, but then again MIME type  
mapping is one of my weak spots in Rails.

> I added  
> “Mime::Type.register\_alias “text/javascript”, :iphonejs” to  
> mime\_types.rb and put “format.iphonejs” in the respond\_to block, but  
> now I get a 406 Not Acceptable error.

Did you restart the server? If I remember correctly, defined MIME types  
are only loaded at server startup.

> Am I doing it wrong?

You must be, since you’re not getting the right result. 😃

But I’m not sure where the problem is, if restarting the server doesn’t  
do the trick.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 30, 2009, 10:23pm UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/5 "2009-04-30T22:23:19Z")

</div>

I made a new app and I started from scratch. After following the steps  
in my previous posts, I still get 406 errors. This is so strange; it  
seems like I’m the only one in the world having this problem with 2.3!  
Anything else I can do?

On Apr 19, 3:25 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 19, 2009, 4:03am UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/6 "2009-04-19T04:03:41Z")

</div>

Thanks for the advice; it worked fine pre-2.3 though. I added  
“Mime::Type.register\_alias “text/javascript”, :iphonejs” to  
mime\_types.rb and put “format.iphonejs” in the respond\_to block, but  
now I get a 406 Not Acceptable error. Am I doing it wrong?

On Apr 17, 9:13 am, Marnen Laibow-Koser \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 30, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/7 "2009-04-30T22:30:41Z")

</div>

Andrew wrote:

> I made a new app and I started from scratch. After following the steps  
> in my previous posts, I still get 406 errors.  
> […]

Hmm. I forgot to ask: what’s the name of the RJS file? I should be  
something.iphonejs.rjs…

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![andrew](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@andrew](https://www.ruby-forum.com/u/andrew)\
**Post date:** [April 30, 2009, 10:34pm UTC](https://www.ruby-forum.com/t/rjs-not-rendering-correctly-when-mime-type-is-iphone/164688/8 "2009-04-30T22:34:28Z")

</div>

Yes, I have tried it with the file named something.iphonejs.rjs.

On Apr 30, 1:30 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
