ZK Zhao
ZK Zhao

Reputation: 21523

Rails: automatically do not render html element when in mobile?

When I'm developing a responsive Rails site, I use something like hidden-xs(from Twitter Bootstrap), to hide some content when user using a mobile device.

However it still generate traffic, and make the page load slower. So I end up with a helper <% if mobile? %> to determine whether to generate it or not.

I think adding both hidden-xs and <% if mobile? %> is quite repetative, and wondering if it is possible that when add a hidden-xs css class, I can make Rails do not render the content at all? so make the webpage load a bit faster?

Additional question: Is it a good practice to use <% if mobile? %> to trim the content in Rails view? Because I feel there maybe too much logic there.

Upvotes: 1

Views: 254

Answers (1)

andreanne.wintheiser
andreanne.wintheiser

Reputation: 386

You can try to use browser gem. It should help to detect requests from mobile devices.

About better place. Look at Variants functionality from Rails 4. I think it should help to split your markup on two parts: mobile and desktop.

Upvotes: 1

Related Questions