0

I have an index.js in place which is fired ok from desktop but gives the following error when I try to open from Mobile Safari.

ActionView::MissingTemplate: Missing template ads/thumbs/index, application/index with {:locale=>[:es, :en], :formats=>[:mobile], :handlers=>[:erb, :builder, :haml]}. Searched in: * "/app/app/views" * "/app/vendor/bundle/ruby/1.9.1/gems/devise-1.5.2/app/views" * "/app/app/views"

I'm not sure why it is looking for a :formats=>[:mobile] when there's no such a file in the folder.

Same thing happens after trying to sign in with devise from mobile phone. I tries to render a nonexistent create.mobile.haml file.

P.S. Is there a way to make :mobile views to fallback default :html views when not found? That would make the trick.

Community
  • 1
  • 1
Martin
  • 11,216
  • 23
  • 83
  • 140

2 Answers2

0

You should in general respond with content-type specific views. In this case, a simple way to get past this short-term issue is to rename index.js to index.mobile.js.

Rails attempts to render views that are specific to the content-type requested -- for example, index.html.haml when html is requested or show.json.haml if you request json. In this case the content-type requested is :mobile.

In the long run you should develop views that will be sent back when different content types are requested.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
  • Yes, I know that, but the app is asking for `:mobile` only on that page. In the rest of the app I don't have any `:mobile` version and is rendering the regular views with no problems. Is there any solution to this that doesn't involve copy again the file and rename it? – Martin May 10 '12 at 01:23
  • Sorry - not trying to imply you didn't understand. I didn't know how much to assume you understood. There are lots of people here with less knowledge. – Kevin Bedell May 10 '12 at 01:55
  • Isn't this against the Rails principle of Don't repeat yourself? If I need to add a mobile version of every view I would end up with a lot of repetition for sure. I'm still looking for a more a-la Rails approach. – Martin May 21 '12 at 09:16
  • Not really, it's the design of rails to allow you to have controllers render different views based on content type -- json, xml, html, js, etc are fundamentally different content types and require different content to be returned. This is the essence of the Model-View-Controller architecture that Rails is based on. – Kevin Bedell May 21 '12 at 14:39
  • Is there a way to make `:mobile` views to fallback default views when not found? That wold make the trick. – Martin May 21 '12 at 21:34
0

Here's a simple solution.

class ApplicationController
    ...
    def formats=(values)
        values << :html if values == [:mobile]
        super(values)
    end
    ...
end

It turns out Rails (3.2.11) already adds an :html fallback for requests with the :js format. Here's ActionView::LookupContext#formats=

# Override formats= to expand ["*/*"] values and automatically
# add :html as fallback to :js.
def formats=(values)
  if values
    values.concat(default_formats) if values.delete "*/*"
    values << :html if values == [:js]
  end
  super(values)
end

So you can override #formats= yourself and it will be conceivably no more gross and hacky than the existing Rails implementation.

Will Madden
  • 6,477
  • 5
  • 28
  • 20