Comments (13)
Try this code, maybe it will fix your issue:
class ApplicationController < ActionController::Base
protect_from_forgery
has_mobile_fu # Detect the device type
before_filter :set_request_format
def set_request_format
request.format = :mobile if is_mobile_device?
end
end
from mobile-fu.
Great! Thanks for the solution.
from mobile-fu.
@dmfrancisco You just saved my bacon! Thanks :-)
Seeing as jquery-mobile has just been released to v. 1.0 and can be expected to grow a lot, I'm adding this to the wiki
from mobile-fu.
Right, setting the request format to mobile definitely solve the issue, but why checking the ajax request at the first place in the method set_mobile_format? @benlangfeld, can you comment?
Many thanks.
from mobile-fu.
I can't comment on the behaviour of mobile-fu as a plugin. If you find any issues with the gem, please file them on my fork as the code is completely different.
from mobile-fu.
But both the gem and the plugin share such method?
https://github.com/benlangfeld/mobile-fu/blob/master/lib/mobile-fu.rb#L66
from mobile-fu.
@gareth (benlangfeld@93d875d) or @revgum (benlangfeld@41dd15c) might recall the reason for this
from mobile-fu.
Afraid I can't shed any light on this. My commit was only to make force_mobile_format
use the same flow as set_mobile_format
(#16) - I don't remember knowing why the .xhr?
check was already there.
from mobile-fu.
I think I am having a similar problem. I wanted to add some more details.
When using mobile_fu everything works fine until I try some ajax requests. I have an ajax action def ajax_select_user, after completion I call render
respond_to do |format|
format.js { render select_user }
end
This code will call the associated select_user.js.erb which is expected. Inside select_user.js.erb I have some javascript to fill in some HTML. I call some partials in the .js.erb files
$("#selected_user").replaceWith("<%= escape_javascript( render(:partial => "selected_user") ) %>")
Now, I want selected_user.mobile.erb to be displayed since this is a mobile ajax request. selected_user.html.erb is called instead.
from mobile-fu.
Ok I have some more details on this. As it is mobile_fu will have difficulty with the scenario I put up.
In an XHR request the type will be set to :js not :mobile. This is desired because we need the correct javascript response type. When in :js mode if you call render the ActionView will first look for :js (this is good for calling your .js.erb file) and will also look for :html. :html is hardcoded here
https://github.com/rails/rails/blob/master/actionpack/lib/action_view/lookup_context.rb
# 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
Ideally it would be nice to change that fallback value dynamically somehow. From :html to :mobile if it's a mobile site. I'm not really sure how that could be done.
I have found a workaround. I was able to add this code to the to of my .js.erb view template.
<% self.formats = [:js, :mobile] %>
I imagine you could do something like this at the top of all of the .js.erb files used in ajax requests..
<% self.formats = [:js, :mobile] if is_mobile_device? %>
from mobile-fu.
@lyslim if you set it to :mobile instead of :js then ajax requests don't work right.
from mobile-fu.
Yes, but I guess this could be done on controller side by calling the class method respond_to :mobile, :html, :js
?
PS, I found setting the format directly also won't work when browsing full site from mobile device, as then we actually need html format.
from mobile-fu.
This project is abandoned. Please see the active fork at http://github.com/benlangfeld/mobile-fu. Please test with the released gem and master branch of the new home for the project, and file an issue on the other repo if you still have problems.
See #40.
from mobile-fu.
Related Issues (20)
- set_mobile_format's XHR filter breaks jQuery Mobile HOT 8
- Plugin not Found HOT 1
- Does not load layout HOT 3
- Gem differs from plugin... issues with gem. HOT 4
- Exclude iPad from mobile view HOT 16
- is_mobile_view? is now in_mobile_view? HOT 7
- breaks assets pipeline HOT 2
- Should session[:mobile_view] be checked in is_mobile_device? HOT 3
- fallback to html HOT 11
- rails 3.1 and 3.2 asset pipeline HOT 3
- IE Browsers and False Positives HOT 3
- update regexes list with the ones from Mobile_Detect HOT 1
- how to design screen size? HOT 1
- how to contain view mobile in a new folder HOT 2
- devise not render view mobile when click link HOT 1
- Please close this repo or add link to https://github.com/benlangfeld/mobile-fu HOT 6
- Broken link
- Rails 5 deprecation warning on "before_filter" HOT 1
- Mobile_fu shows Mobile Site to Search Bots (googlebot) instead of Desktop Site HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mobile-fu.