Forum: Ruby on Rails RJS not rendering correctly when mime-type is 'iphone'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrew (Guest)
on 2009-04-04 03:01
(Received via mailing list)
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.
Andrew (Guest)
on 2009-04-17 10:05
(Received via mailing list)
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?
Marnen L. (Guest)
on 2009-04-17 20:13
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
removed_email_address@domain.invalid
Andrew (Guest)
on 2009-04-19 06:03
(Received via mailing list)
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-
Marnen L. (Guest)
on 2009-04-20 02:25
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. :D

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
removed_email_address@domain.invalid
Andrew (Guest)
on 2009-05-01 00:23
(Received via mailing list)
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-
Marnen L. (Guest)
on 2009-05-01 00:30
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
removed_email_address@domain.invalid
Andrew (Guest)
on 2009-05-01 00:34
(Received via mailing list)
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-
This topic is locked and can not be replied to.