(缺少)在新的 Rails 3 应用程序中支持 iPhone 的步骤

发布于 2024-11-16 01:50:19 字数 894 浏览 4 评论 0原文

我缺少哪一步?返回到我的 iPhone 的视图来自 application.html.erb 和 index.html.erb

步骤 1:在 config/initializers/mime_types.rb 中取消注释 iPhone 的声明行:

Mime::Type.register_alias "text/html", :iphone

步骤 2:复制 app/views/layouts/application.html.erb 将其称为 application.iphone.erb (我喜欢将标题更改为特定于您的 iPhone 的内容,这样您就可以立即看到正确的正在使用布局)

<title>My iPhone Tasks</title>

第 3 步:在控制器中复制必要的视图文件,将它们命名为“index.iphone.erb”

第 4 步:决定是否坚持使用专门调用格式的 respond_to 块的 Rails 2 模型iPhone 或切换到使用 respond_with 调用的更 DRY 方法。这就是我所做的,呃尝试过;-)

步骤 4a:将 respond_to 块添加到控制器:

class TasksController < ApplicationController
respond_to :html, :iphone

步骤 4b:干燥你的方法,例如:

def index
  @tasks = Task.all
  respond_with (@tasks)
end

步骤 5:重新启动服务器并从 iPhone 上点击应用程序。

What step am I missing? The view being returned to my iPhone is coming from application.html.erb and index.html.erb

Step 1: In config/initializers/mime_types.rb uncomment the declaration line for the iPhone:

Mime::Type.register_alias "text/html", :iphone

Step 2: Make a copy of app/views/layouts/application.html.erb calling it application.iphone.erb (I like to change the title to something specific to your iPhone so you can see immediately that the correct layout is being used)

<title>My iPhone Tasks</title>

Step 3: Make copies of the necessary view files in your controllers, calling them things like index.iphone.erb

Step 4: Decide whether to stick with the Rails 2 model of respond_to blocks that specifically call out a format of iphone or switch to the more DRY approach that uses the respond_with call. That's what I've done, er tried ;-)

Step 4a: Add to your controller the respond_to block:

class TasksController < ApplicationController
respond_to :html, :iphone

Step 4b: DRY up your methods, such as:

def index
  @tasks = Task.all
  respond_with (@tasks)
end

Step 5: Restart your server and hit the app from your iPhone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-11-23 01:50:19

您必须为您的移动 mime 类型编写一个响应程序,respond_with 才能正常工作。

在application_controller.rb中定义响应者:

  def self.responder
    MobileResponder
  end

class MobileResponder < ActionController::Responder

  def to_format
    super
  rescue ActionView::MissingTemplate => e
    if request.format == "iphone"
      navigation_behavior(e)
    else
      raise unless resourceful?
      api_behavior(e)
    end
  end
end

You have to write a responder for your mobile mime type for respond_with to properly work.

Define the responder in application_controller.rb:

  def self.responder
    MobileResponder
  end

class MobileResponder < ActionController::Responder

  def to_format
    super
  rescue ActionView::MissingTemplate => e
    if request.format == "iphone"
      navigation_behavior(e)
    else
      raise unless resourceful?
      api_behavior(e)
    end
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文