Coder Social home page Coder Social logo

Comments (13)

dmfrancisco avatar dmfrancisco commented on July 17, 2024

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.

pauloconp avatar pauloconp commented on July 17, 2024

Great! Thanks for the solution.

from mobile-fu.

houen avatar houen commented on July 17, 2024

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

lyslim avatar lyslim commented on July 17, 2024

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.

benlangfeld avatar benlangfeld commented on July 17, 2024

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.

lyslim avatar lyslim commented on July 17, 2024

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.

benlangfeld avatar benlangfeld commented on July 17, 2024

@gareth (benlangfeld@93d875d) or @revgum (benlangfeld@41dd15c) might recall the reason for this

from mobile-fu.

gareth avatar gareth commented on July 17, 2024

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.

pipplo avatar pipplo commented on July 17, 2024

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.

pipplo avatar pipplo commented on July 17, 2024

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.

pipplo avatar pipplo commented on July 17, 2024

@lyslim if you set it to :mobile instead of :js then ajax requests don't work right.

from mobile-fu.

lyslim avatar lyslim commented on July 17, 2024

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.

benlangfeld avatar benlangfeld commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.