Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single view template for multiple request formats

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.haml. 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.

like image 724
buddhamagnet Avatar asked Dec 08 '25 15:12

buddhamagnet


1 Answers

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.

like image 107
Jimmy Avatar answered Dec 11 '25 13:12

Jimmy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!