English 中文(简体)
处理 :js 格式,在铁路中使用黑花
原标题:Handling :js format with mobylette in Rails

我使用 mobylette 宝石管理 :mobile 格式视图。

我有一个控制器, 使一个 javascript 文件 (/ app/views/ photos/index.js.erb ) 。

class Photos < ApplicationController
  respond_to :js
  def index 
    ...
  end
end

如果我从桌面访问 /photos /photos.js /photos.js 似乎效果良好(我看到.js输出),但如果我从移动设备访问,我就会发现以下错误:

Template is Missing
Missing template photos/index, application/index with locale {:locale => [:en], :formats => [:mobile], :handlers => [:erg,:coffee,:haml]}. Searched in: *"/Users/user/site/app/views" * "/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.2/app/views" * "/Users/user/site/app/views"

我在 application_controller.rb 中有以下代码:

before_filter :set_request_format, :set_format_fallbacks

def set_request_format
  if is_mobile_request? && request.format.to_s == "text/html"
      request.format = :mobile
  elsif is_mobile_request? && request.format.to_s == "text/javascript"
      request.format = :mobilejs
  end
end

def set_format_fallbacks
  if request.format == :mobile
    self.formats = [:mobile, :html]
  elsif request.format == :mobilejs
    self.formats = [:mobilejs, :js]
  end
end

并在 /config/ reminizers/mime_ types.rb 中插入此内容。

Mime::Type.register_alias "text/html", :mobile
Mime::Type.register_alias "text/javascript", :mobilejs

出于某种原因,当 [:mobile] 格式应该改为 :js (因为 :moblesjs 不存在) 时, 格式仍然在创建中。

知道该怎么工作吗?

问题回答

Maybe not the prettiest of solutions, but I did not find a mobylette-supported way to do it either. I used a similar way, but could not use request.format in your before_filter, I had to use request.filtered_parameters["format"] instead. Still looking for the pretty version.

index.js.erb

变换为

index.erb




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...