I have a Rails 3.2 app where a user clicks an image to select it. Clicking the image triggers an ajax POST request to a controller with an action that updates a boolean on the photo’s record to select it. It works perfectly on my local machine with Pow. However, this breaks with a 404 error on our hosting, Heroku. The record does not get updated. I’ve tried countless changes to the ajax request content types and even changed the ajax request url to suffix .html to force a request for a content type I know exists but to no avail.
I’ve used the web inspector to analyse the requests and they’re completely identical. I’m stumped.
I have the following controller:
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
The following 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');
}
);
}
And these routes:
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 routes gives:
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
Thanks!
UPDATE:
Heroku’s logs are showing the following:
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....
Ok, found it. The add route is pointing at
plugins/instagram_photos#addbut the controller isclass InstagramPhotosController < ApplicationControlleri.e. NOT extending a base controller calledPlugins::BaseController. No idea why this worked just fine in development.It pays to look at the logs, kids.