Ajax POST请求在开发中工作,在Heroku上失败了404

时间:2012-06-19 03:58:56

标签: ruby-on-rails jquery heroku

我有一个Rails 3.2应用程序,用户点击图像进行选择。单击该图像会触发对控制器的ajax POST请求,其操作会更新照片记录上的布尔值以选择它。它在Pow的本地机器上完美运行。但是,这打破了我们的主机Heroku上的404错误。记录未更新。我已经尝试了对ajax请求内容类型的无数更改,甚至将ajax请求URL更改为后缀.html以强制请求我知道存在的内容类型但无济于事。

我使用网络检查器分析请求,它们完全相同。我很难过。

我有以下控制器:

class InstagramPhotosController < ApplicationController

  before_filter :authenticate_user!
  before_filter :set_account
  prepend_view_path('app/views/plugins')

  respond_to :html

  def add
    @account.plugin.instagram_photos.find(params[:id]).update_attribute('selected', true)
    render :nothing => true
  end

  def remove
    @account.plugin.instagram_photos.find(params[:id]).update_attribute('selected', false)
    render :nothing => true
  end

  private

  def set_account
    @account = current_user.accounts.find_by_subdomain(params[:account])
  end
end

以下javascript:

handle_change_display = function(e) {
  $('#items').show().on('click', '.instagram-image', function(e){
    if ($(this).hasClass('selected')) {
      $.publish("/remove_item", [ this ]);
    } else {
      $.publish("/add_item", [ this ]);
    }
  });
}

handle_add_item = function(e, image) {
  $.post('instagram/photos/add.html', {
      id: $(image).attr('data-photo-id')
    },
    function() {
      $(image).addClass('selected');
    }
  );
}

这些路线:

scope :account, :path => '/:account/plugins' do
  post 'instagram/photos/add'    => 'plugins/instagram_photos#add',    :as => 'instagram_photos_add'
  post 'instagram/photos/remove' => 'plugins/instagram_photos#remove', :as => 'instagram_photos_remove'
end

Rake路线给出:

instagram_photos_add POST   /:account/plugins/instagram/photos/add(.:format)        plugins/instagram_photos#add
instagram_photos_remove POST   /:account/plugins/instagram/photos/remove(.:format)     plugins/instagram_photos#remove

谢谢!

更新:

Heroku的日志显示如下:

2012-06-19T04:10:16+00:00 app[web.1]: Started POST "/tim/plugins/instagram/photos/add.html" for 75.158.30.18 at 2012-06-19 04:10:16 +0000
2012-06-19T04:10:16+00:00 app[web.1]: 
2012-06-19T04:10:16+00:00 app[web.1]: ActionController::RoutingError (uninitialized constant Plugins::InstagramPhotosController):
2012-06-19T04:10:16+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:229:in `block in constantize'
2012-06-19T04:10:16+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:228:in `each'
2012-06-19T04:10:16+00:00 app[web.1]:   vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.2/lib/active_support/inflector/methods.rb:228:in `constantize'
snipped....

1 个答案:

答案 0 :(得分:3)

好的,找到了。添加路由指向plugins/instagram_photos#add,但控制器为class InstagramPhotosController < ApplicationController,即不扩展名为Plugins::BaseController的基本控制器。不知道为什么这在开发中工作得很好。

查看日志,孩子们是值得的。