Rails3和Respond_with问题

时间:2011-01-13 14:04:54

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

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

第一个用于普通用户,第二个用于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

2 个答案:

答案 0 :(得分:1)

我自己解决了。

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

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

答案 1 :(得分:0)

如果你这样做:

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