ruby-on-rails-3 - Rails 3.1 respond_to :html with :except

标签 ruby-on-rails-3

我的 Controller 中有以下内容:

respond_to :html, :except => :some_action
respond_to :json, :xml

如果您在浏览器中点击 :some_action 路由(使用 Chrome 测试),您会收到 406 Not Acceptable 响应。有没有办法在 Rails 中“捕获”这个并做其他事情(比如重定向)?

此外,我试图避免使用respond_to 的 block 形式。我只是好奇是否有办法处理这种情况。

最佳答案

检查一下:http://ryandaigle.com/articles/2009/8/6/what-s-new-in-edge-rails-cleaner-restful-controllers-w-respond_with

有一些关于操作覆盖的内容:

class UsersController < ApplicationController::Base

  respond_to :html, :xml, :json

  # Override html format since we want to redirect to a different page,
  # not just serve back the new resource
  def create
    @user = User.create(params[:user])
    respond_with(@user) do |format|
      format.html { redirect_to users_path }
    end
  end
end

关于ruby-on-rails-3 - Rails 3.1 respond_to :html with :except,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6853907/

相关文章:

ruby-on-rails - 为什么有时会得到 “undefined method ` videoId'”?

ruby - 如何在 Rails 3.0.3 中设置邮件拦截器?

ruby-on-rails - 避免在 meta_search 中选择 * 查询

ruby-on-rails-3 - 最新 Rails 3:ActionView::Template::Error(无法解析第 105 行第 13 列的 YAML)

ruby-on-rails-3 - 设计可邀请的节目邀请人姓名或电子邮件

ruby-on-rails - Rails 3.1 -- 不能批量分配 protected 属性(即使添加到 attr_accessible)

ruby-on-rails - 添加 Accepts_nested_attributes_for 时,Fields_for 消失

mysql - 导轨 : Querying with an Array

ruby-on-rails - Rails Asset Pipeline 更好的错误或堆栈跟踪

ruby-on-rails - Rspec with rails 3.1 给出了弃用警告 ActiveRecord::Associations::AssociationCollection 已弃用?