【问题标题】:rails3, jquery mobile, and remote => true ajax calls processing as HTMLrails3、jquery mobile 和 remote => true ajax 调用处理为 HTML
【发布时间】:2012-03-01 09:56:36
【问题描述】:

我在这里来回浏览了一些帖子,还查看了 Railscast 的移动检测 (#199)。我有一个使用 jquery 和 ajax 以及 format.js 并且工作正常的 rails3 应用程序。现在我正在尝试将应用程序扩展到移动设备。

我正在使用 jquery mobile 并且一切正常,但是当我在 link_to 上使用远程 => true 开关时,它不会将请求作为 JS 处理(通常在桌面版本中执行)。

这里是链接代码:

<%= link_to 'Up', '/posts/' + String(rbpost.id) + '/upvote', :remote => true, :class => 'upvote', "data-role" => "button", "data-icon" => "arrow-u", "data-iconpos" => "notext" %>

它正在处理为 HTML

Started GET "/posts/3/upvote" for 127.0.0.1 at 2012-02-08 11:37:59 -0500

  Processing by PostsController#upvote as HTML

。 . .并抱怨没有模板:

ActionView::MissingTemplate (Missing template posts/upvote, application/
upvote with {:handlers=>[:erb, :builder, :coffee], :formats=>[:mobile], :locale=
[:en, :en]}. Searched in:
  * "C:/rubydemo/testapp/app/views"
):
  app/controllers/posts_controller.rb:74:in `upvote'


下面是代码的编码方式:

application_controller.rb:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :adjust_format_for_mobile

private
  # Set iPhone format if request 
  def adjust_format_for_mobile
    if mobile_request?
        if request.format == :js
            request.format = :mobilejs
        else
            request.format = :mobile
        end
    end
  end

# Return true for mobile requests 
  def mobile_request?
    return request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/]
    #return true
    #I set this to return true always when I'm debugging on the desktop
  end  
end

mime_types.rb:

# Be sure to restart your server when you modify this file.

# Add new mime types for use in respond_to blocks:
# Mime::Type.register "text/richtext", :rtf
 Mime::Type.register_alias "text/html", :mobile
 Mime::Type.register_alias "text/javascript", :mobilejs

posts_controller.rb:

  def upvote
    @post = Post.find(params[:id])
    @post.upvote
    @post.save

    respond_to do |format|
      format.js 
      format.mobilejs
      format.html { redirect_to @post }
      format.json { render json: @post }
    end
  end

我的视图/帖子目录中有 upvote.js.erb 和 upvote.mobilejs.erb。它正在注册 :mobile 类型,但由于某种原因在需要的时候没有做 javascript 的事情。有什么想法吗?

谢谢

【问题讨论】:

  • 我在使用常规应用程序时遇到了完全相同的问题:stackoverflow.com/questions/9144546/… 我最终创建了一个新项目,复制了代码并且它运行良好,所以我只能想象我搞砸了的配置文件。我会密切关注这一点的。祝你好运!
  • 谢谢 - 我在想重新开始可能是个好主意。不断粗心的配置更改会造成混乱。我猜。
  • 我发现了我的问题,我没有 //= require jquery //= require jquery_ujs //= require_tree 。可用的。是否有可能您的移动 .js 文件没有被预编译?

标签: jquery ajax ruby-on-rails-3 mobile


【解决方案1】:

我猜这是因为 JQuery Mobile 和 Ruby on Rails Ajax 处理搞砸了。我通过显式禁用表单的 JQM ajax 功能解决了同样的问题:

<%= form_for(AgendaItem.new, :remote => true, :html =>{ "data-ajax" => false}) do |f| %>

【讨论】:

  • 根据您要远程执行的操作,该方法的签名会略有变化。例如,我在我的情况下使用form_tag,而在 HAML 中签名变为= form_tag(update_group_path, :remote =&gt; true, :data =&gt; {:ajax =&gt; 'false'})。确保将 :remote 选项和 HTML 选项放在您使用的方法所期望的位置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-04
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-27
  • 1970-01-01
相关资源
最近更新 更多