开发者

Single view template for multiple request formats

开发者 https://www.devze.com 2023-02-09 04:03 出处:网络
I am involved in the build of an HTML5 app and we have created a custom MIME type (mobile) for iPhone, Android etc. So for example we have index.html.haml and index.mobile.ham开发者_StackOverflow社区l

I am involved in the build of an HTML5 app and we have created a custom MIME type (mobile) for iPhone, Android etc. So for example we have index.html.haml and index.mobile.ham开发者_StackOverflow社区l. In many cases the output will be the same - so my question is, how can I create a view file that is used by both mobile and html request formats? And in the cases where the view is different, select the correct html or mobile view?

I have been digging and can't seem to find a clean and elegant solution.


You can handle both the regular and mobile requests in the same respond_to block and explicitly set the template to use. For example:

respond_to do |format|
  format.any(:html, :mobile)
    render :template => "action.html.erb"
  end
end

However, I read an article recently which eschews a mobile-specific request format for a sort of "cascading" of views. You might want to consider that as well: Mobile Devices and Rails: Maintaining your Sanity.

0

精彩评论

暂无评论...
验证码 换一张
取 消