ユーザーが画像をクリックして選択するRails 3.2アプリがあります。画像をクリックすると、コントローラーへの ajax POST リクエストがトリガーされ、写真のレコードのブール値を更新して選択するアクションが実行されます。Pow を使用しているローカル マシンで完全に動作します。ただし、これはホスティングしている Heroku の 404 エラーで壊れます。レコードは更新されません。私は ajax リクエストのコンテンツ タイプに数え切れないほどの変更を試み、さらに ajax リクエストの URL をサフィックス .html に変更して、存在することがわかっているコンテンツ タイプのリクエストを強制しましたが、役に立ちませんでした。
Web インスペクターを使用してリクエストを分析しましたが、それらは完全に同一です。私は困惑しています。
私は次のコントローラーを持っています:
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
次のジャバスクリプト:
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....