respond_to只有format.js为所有mime类型 [英] respond_to only format.js for all mime types

查看:262
本文介绍了respond_to只有format.js为所有mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制器respond_to format.js,但是,大多数请求假定旧的format.html仍然存在,并抛出一个404异常。如何捕获控制器上的所有MIME请求,并将它们仅重定向到format.js?

I have a controller which respond_to format.js, however, most request assume the old format.html still exists and throws a 404 exception. How do I capture all MIME request on the controller and redirect them only to format.js?

以下是当前控制器操作

def search
  respond_to do |format|
    unless @search.nil?
      format.js { render :partial => '/search/search_form', :status => 200 }
    else
      format.js { render :partial => '/search/not_exist', :status => 500 }
    end
  end
end

(我知道这是无效的,只是为了演示)。

I'm trying to do something like this, (I know this is invalid, just for demonstration).

def search
  respond_to(:html) do |format|
    unless @search.nil?
      format.js { render :partial => '/search/search_form', :status => 200 }
    else
      format.js { render :partial => '/search/not_exist', :status => 500 }
    end
  end
end


推荐答案

如果所有请求都只有js,只需取出整个respond_to结构:

If all requests should only be js, just take out the whole respond_to structure:

def search
  unless @search.nil?
    render :partial => '/search/search_form', :status => 200
  else
    render :partial => '/search/not_exist', :status => 422
  end
end

(注意:更改为422个不可处理实体一个语义问题与提交。500通常保留为服务器错误,如在崩溃,堆栈转储等)

(note: change to 422 unprocessable entity to indicate a semantic problem with the submission. 500 is usually reserved for server errors, as in, crashes, stack dumps, etc)

这篇关于respond_to只有format.js为所有mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆