ruby-on-rails - Ruby on Rails 中的奇怪重定向

标签 ruby-on-rails ruby-on-rails-3.2 memcached rails-activerecord rails-routing

我一般都会遇到奇怪的重定向。它适用于开发,但不适用于生产。以下是更新的回复。同样的事情也发生在创建上。

发展

Redirected to http://localhost:3000/trackers
Completed 302 Found in 43ms (ActiveRecord: 18.7ms)
Started GET "/trackers" for 127.0.0.1 at 2014-01-06 06:41:02 -0600

生产

Redirected to http://example.com/trackers
Completed 302 Found in 218ms (ActiveRecord: 63.9ms)
Started GET "/" for [IP} at 2014-01-05 20:15:33 +0000

routes.rb(相关)

              trackers GET    /trackers(.:format)                       trackers#index
                       POST   /trackers(.:format)                       trackers#create
           new_tracker GET    /trackers/new(.:format)                   trackers#new
          edit_tracker GET    /trackers/:id/edit(.:format)              trackers#edit
               tracker GET    /trackers/:id(.:format)                   trackers#show
                       PUT    /trackers/:id(.:format)                   trackers#update
                       DELETE /trackers/:id(.:format)                  trackers#destroy

这是我的跟踪器 Controller

class TrackersController < ApplicationController
 before_filter :authenticate_user!
load_and_authorize_resource

def create
  @tracker = params[:tracker]
  @user = current_user
  if @user.trackers.create(@tracker)
   redirect_to trackers_path, :notice => "New Tracker Created!"
  else
   redirect_to trackers_path, :notice => "Tracker Could not be created."
  end
end

def update
  @tracker = Tracker.find(params[:id])
  if @tracker.update_attributes(params[:tracker])
   redirect_to trackers_path, :notice => "Tracker Updated!"
  else
   redirect_to trackers_path(params[:id]), :notice => "Unable to Update Tracker"
  end
end

end

NGINX

upstream unicorn{
server unix:/tmp/unicorn.legalleads.sock fail_timeout=0;}


server {
listen      80;
server_name example.com;
return 301 https://$host;}


server {

listen 443 default;
server_name example.com;
root /home/a/apps/legalleads/public;
try_files $uri/index.html $uri @unicorn;

ssl on;
ssl_certificate /etc/nginx/ssl/com.crt;
ssl_certificate_key /etc/nginx/ssl/server.key;


location ^~ /assets/ {
  gzip_static on;
  expires max;
  add_header Cache-Control public;
}

location @unicorn {
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header Host $http_host;
 proxy_redirect off;
 proxy_pass http://unicorn;
}

error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}

最佳答案

您以与 HTTP 规范不兼容的方式将渲染和重定向链接在一起。从您提供的 Controller 的有限部分来看,我假设在登录时发出 POST 请求,该请求会输入重定向,然后输入另一个重定向或渲染。最后的重定向/渲染调用是罪魁祸首。

If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

来自HTTP/1.1 Status Code Definitions .

您应该跟踪所有回调中的逻辑,以确保仅在 POST 请求结束时调用渲染/重定向一次。

关于ruby-on-rails - Ruby on Rails 中的奇怪重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928413/

相关文章:

ruby-on-rails - rails_helper.rb 文件在哪里?

ruby-on-rails - 在 Rails 中存储全局共享数据结构的位置

ruby-on-rails-3 - Rails 3.2、activeresource 和证书认证

performance - 证明 memcache 比文件系统缓存更快?

python - 在 Google App Engine 上同步 Memcache 和 Datastore

javascript - 为什么我的 CSS 不能在我的 Rails 应用程序上运行?

ruby-on-rails - 设置IIS和jRuby的说明

ruby-on-rails - 如果数据库关闭,为什么准系统 Rails Controller 会阻塞?

ruby-on-rails - 在 Rails View 中查找模式并替换为链接的正确方法

ruby - Redis存储地理信息,