Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 对所有mime类型只响应format.js_Ruby_Ruby On Rails 3_Controller_Ruby On Rails 3.1 - Fatal编程技术网

Ruby 对所有mime类型只响应format.js

Ruby 对所有mime类型只响应format.js,ruby,ruby-on-rails-3,controller,ruby-on-rails-3.1,Ruby,Ruby On Rails 3,Controller,Ruby On Rails 3.1,我有一个响应format.js的控制器,但是,大多数请求都假设旧的format.html仍然存在,并抛出404异常。如何捕获控制器上的所有MIME请求并将其重定向到format.js 这是当前控制器的操作 def search respond_to do |format| unless @search.nil? format.js { render :partial => '/search/search_form', :status => 200 }

我有一个响应format.js的控制器,但是,大多数请求都假设旧的format.html仍然存在,并抛出404异常。如何捕获控制器上的所有MIME请求并将其重定向到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
我正在尝试这样做,(我知道这是无效的,只是为了演示)


您可以插入到format.html的永久重定向,并使用所需的格式将其循环回控制器。这是一种将RSS提要重定向到atom提要的方法,或者您可能有多种输入格式,但只有一种输出格式

respond_to do |format|
...
format.js { do whatever }
...
format.html { redirect_to path_back_here(:format => :js) }

用您正在使用的任何路径(搜索路径?)替换此处的路径(返回路径)

如果所有请求都应该是js,只需取出整个respond to结构:

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

(注意:更改为422 unprocessable entity表示提交中存在语义问题。500通常保留用于服务器错误,如,崩溃、堆栈转储等)

format.all{…}有什么问题?
def search
  unless @search.nil?
    render :partial => '/search/search_form', :status => 200
  else
    render :partial => '/search/not_exist', :status => 422
  end
end