【问题标题】:Rails3 and Respond_with problemRails3 和 Respond_with 问题
【发布时间】:2011-01-13 14:04:54
【问题描述】:

我有一个应用程序,上面有两个用户界面。

第一个是普通用户,第二个是iphone用户。

一切正常,直到我在控制器中重构我的代码以使用respond_with 声明性而不是respond_to。

该应用程序仍然适用于 html 界面(:format => :html),但不适用于 iphone 界面(:format => :iphone)。

在 iphone 上,当我执行以下操作(:index、:new、:edit、:show)时,它会起作用。

但是当我这样做(:create,:update,:destroy)时,我收到错误消息说找不到模板(例如create.iphone.haml)。

在我的控制器上我有

respond_to :html, :iphone

然后例如,编辑和更新操作

def edit
    @refund = Refund.find(params[:id])
    respond_with(@refund)
  end
  def update
    @refund = Refund.find(params[:id])
    if @refund.update_attributes(params[:refund])
      flash[:notice] = 'Refund was successfully updated.'
    end
    respond_with(@refund, :location => project_refunds_path(@project))
  end

事实上,我希望 :iphone 格式被处理为 :html 是 ... 而不是通过调用 to_format 方法,因为它在 doc 中指定。

【问题讨论】:

    标签: ruby-on-rails-3 respond-to respond-with


    【解决方案1】:

    自己解决了。

    只需将其添加到初始化文件中:

    ActionController::Responder.class_eval do
      alias :to_iphone :to_html
    end
    

    【讨论】:

      【解决方案2】:

      如果你这样做:

      respond_with(@refund, :location => project_refunds_path(@project)) do |format|
        format.iphone { whatever you had here before refactoring }
      end
      

      【讨论】:

      • 是的,这可能是一个解决方案。但这对我来说还不够干。 Iphone 和 html 格式与它们应该响应的方式相同。如果可能的话,我正在寻找更好的方法。无论如何谢谢;)
      猜你喜欢
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多