Rails3, jquery mobile and remote => true ajax call handling like HTML

I went back and forth after a few messages, and also checked for Railscast detection for mobile devices (# 199). I have a rails3 application that uses jquery and ajax and format.js and it works fine. Now I am trying to extend the application to mobile devices.

I use jquery mobile and everything works fine, but when I use remote => true switch on link_to, it does not treat the request as JS (which usually does in the desktop version).

Here is the link code:

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

It is processed as 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 

.,. and complains that there is no template:

 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' 


Here's how things are encoded:

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 

I have upvote.js.erb and upvote.mobilejs.erb in my views / posts directory. It registers: a mobile type, but for some reason does not do javascript when it needs it. Any ideas?

thanks

+4
source share
1 answer

I guess this happens when JQuery Mobile and Ruby on Rails Ajax Handling get confused. I fixed the same problem if I explicitly disabled the ajax JQM function for the form:

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

Source: https://habr.com/ru/post/1395502/


All Articles