ActionView::MissingTemplate: Missing template (Trying to render nonexistent :mobile format )

流过昼夜 提交于 2019-12-25 16:48:50

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10526214/actionviewmissingtemplate-missing-template-trying-to-render-nonexistent-mob

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!