redirect_to使用PATCH动词而不是GET

时间:2014-04-10 21:40:45

标签: ruby-on-rails ajax ruby-on-rails-4 actioncontroller

我正在将我的rails 4应用程序中的一些请求迁移到ajax,并且在重定向方面遇到了一些麻烦。

在主页上,我有一个可以启用/禁用的项目列表,这很容易。这两个链接中都有<%= link_to "disable", toggle_my_class_path(my_class), method: "PATCH", remote: true %>

ApplicationController我有before_action检查当前用户是否已登录,如果没有,则将其重定向到显示登录表单的主页。

def require_login
  unless current_user
    redirect_to root_url, :notice => "Please log in"
  end
end

问题是(我认为)因为ajax调用以某种方式使用PATCH动词而不是GET而导致路由问题。以下是我来自呼叫的日志输出

Started PATCH "/my_class/2/toggle" for 127.0.0.1 at 2014-04-10 16:33:52 -0500
Processing by MyClassController#toggle as JS
  Parameters: {"id"=>"2"}
Can't verify CSRF token authenticity
   (0.1ms)  SELECT COUNT(*) FROM "users"
Redirected to http://localhost:3000/
Filter chain halted as :require_login rendered or redirected
Completed 302 Found in 9ms (ActiveRecord: 1.3ms)


Started PATCH "/" for 127.0.0.1 at 2014-04-10 16:33:56 -0500

ActionController::RoutingError (No route matches [PATCH] "/"):
  actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
  railties (4.0.0) lib/rails/engine.rb:511:in `call'
  railties (4.0.0) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /home/***/.rvm/rubies/ruby-head/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

你可以看到 ActionController :: RoutingError(没有路由匹配[PATCH]“/”):是问题

我不知道如何解决这个问题。我不希望使用PATCH动词但是我似乎找不到在redirect_to中指定动词的方法

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您无法在ajax调用中从服务器端(控制器)重定向。通过javascript检索的所有内容都返回javacript; Web浏览器期待评估一些javascript。

所以你需要使用javascript执行重定向。

例如,您可以更改before_action(未测试的代码):

def require_login
  unless current_user
    if request.xhr?
      flash[:notice] = "Please log in"
      flash.keep(:notice) # Keep flash notice for the redirect
      render js: "window.location = #{root_url.to_json}" # js to be evaluated
    else
      redirect_to root_url, :notice => "Please log in"
    end
  end
end