0

我有一个 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

耙路线给出:

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....
4

1 回答 1

3

好的,找到了。add 路由指向,plugins/instagram_photos#add但控制器class InstagramPhotosController < ApplicationController没有扩展名为Plugins::BaseController. 不知道为什么这在开发中效果很好。

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

于 2012-06-19T04:47:07.663 回答