JQueryの$.ajax
からangularjsの$http
に変更すると、railsのアクションの呼び出しで失敗するケースにはまったのでメモ。
railsのエラー内容(xxxxとかyyyyはアプリ固有の値)
ActionView::MissingTemplate (Missing template sample/index, application/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee]}. Searched in:
* "/home/xxxx/yyyy/app/views"
):
actionpack (4.0.1) lib/action_view/path_set.rb:46:in `find'
actionpack (4.0.1) lib/action_view/lookup_context.rb:115:in `find'
actionpack (4.0.1) lib/action_view/renderer/abstract_renderer.rb:18:in `find_template'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:35:in `determine_template'
actionpack (4.0.1) lib/action_view/renderer/template_renderer.rb:8:in `render'
actionpack (4.0.1) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionpack (4.0.1) lib/action_view/renderer/renderer.rb:23:in `render'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:127:in `_render_template'
actionpack (4.0.1) lib/action_controller/metal/streaming.rb:219:in `_render_template'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:120:in `render_to_body'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:33:in `render_to_body'
actionpack (4.0.1) lib/action_controller/metal/renderers.rb:26:in `render_to_body'
actionpack (4.0.1) lib/abstract_controller/rendering.rb:97:in `render'
actionpack (4.0.1) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.0.1) lib/action_controller/metal/instrumentation.rb:41:in `block (2 levels) in render'
activesupport (4.0.1) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/home/xxxx/.rvm/rubies/ruby-2.0.0-p353/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
テンプレートがないということらしい。
JQueryの$.ajax
のコード
function xhr_get(url) {
return $.ajax({
url: url,
type: 'get',
dataType: 'json'
})
.always(function(){
console.log("success");
})
.fail(function(){
console.log("error");
});
}
angularjsの$http
のコード
app.factory('SampleService', function($http) {
var search = function(params) {
return $http({
method: 'GET',
url:'/sample/index',
params: params,
responseType: 'json'
}).then(function(response) {
return response.data;
});
};
return {
search: search
};
});
原因は、angularjsの$httpのパラメータのurlの最後に.jsをつけると解決。
url:'/sample/index.js',