Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7074193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:01:22+00:00 2026-05-28T06:01:22+00:00

I’m creating a mobile site next to our normal html site. Using rails 3.1.

  • 0

I’m creating a mobile site next to our normal html site. Using rails 3.1. Mobile site is accessed in subdomain m.site.com.

I have defined mobile format (Mime::Type.register_alias “text/html”, :mobile).

In ApplicationController I have “before_filter :mobile_site_before_filter” that recognizes mobile site and sets format according to it.

def mobile_site_before_filter
  if request.subdomains.first == 'm'
    original_format = request.format
    request.format = :mobile
    @mobile_site = true
    request.formats.push(original_format)
  end
end

In layouts I have ‘main.html.erb’ and ‘main.mobile.erb’. So with mobile sites mobile layout is used.

Now it kind of works ok.

In UserController I have index action which chooses index.html.erb or index.mobile.erb automatically. No extra coding in top of mobile-view needed. Success.

But I have lots of other views where the same template could be used to serve inside mobile layouts but with minor changes.

E.g. in MessagesController the same view would be almost fine for mobile

In index.html.erb
Normal user info, common for mobile and html
<%= render(:partial => 'messages') %>

Rendering messages.html.erb, no need for messages.mobile.erb. mobile view can be done with css

<%# By default self.formats = [:html] because this is .html.erb partial, even in mobile site %>
<%= self.formats = [:mobile, :html] if @mobile_site # How to get rid of this? %>
<%= render(:partial => 'vote_form') %>
<!-- rest of page ... -->

Now i want to render vote_form.[mobile|html].erb depending on the site…

Now the index.html.erb partial would be fine to use with mobile if I could just choose between vote_form.html.erb or vote_form.mobile.erb. I can choose to use mobile partial with using
“self.formats = [:mobile, :html] if @mobile_site” in the beginning of index.html.erb . But it feels stupid to write this in beginning of all templates.

So the question is:

  • Where does the self.formats come in views (what sets it originally) and how can I set that it’s always [:mobile, :html] inside the mobile site? Why isn’t it the same as i set in controller before_filter? Can I set it somehow in the controller already?

(minor bonus questions)

  • Is there something wrong in this approach? Using mostly the same html-views and in some specific cases using mobile.erb views instead. Why doesn’t this work by default in rails?

  • Is there other ways to choose to render :mobile views if found with fallback to normal html view? Even across the partials so that html.erb-view tries to use _partial.mobile.erb partials.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:01:23+00:00Added an answer on May 28, 2026 at 6:01 am

    You can register new format for the whole application in your mime type initializers:

     Mime::Type.register_alias "text/html", :mobile
    

    Now you can do something like this in your templates to specify format priority(see How do I render a partial of a different format in Rails?):

    <% self.formats = [:mobile, :html] %>
    

    In this case if there is mobile template it will be used for rendering with fallback to ordinary html template. Now you should only determine if user is browsing via mobile browser and conditionally execute this code. Or you can just assign formats value in ApplicationController filter so correct template will be chosen automaticaly.

    UPDATE:

    It seems like by this time there is no “legal” way to solve this problem using Rails. Here is unclosed issue in the rails repository. There you can find patch that can solve your problem, but it uses private Rails API, so it can be unstable.

    Also you can try implement your own view resolver that possibly can solve the problem: http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a French site that I want to parse, but am running into
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.