Ruby on rails routes.rb显式选择模板

Ruby on rails routes.rb显式选择模板,ruby-on-rails,json,routes,Ruby On Rails,Json,Routes,我想创建一个.erb文件,作为返回JSON的许多小操作的输出。因此,路线类似于: map.json 'contacts_json', :controller => 'contacts', :action => 'get_json' map.json 'cal_json', :controller => 'calendar', :action => 'get_json' ... 但这需要我创建一个联系人erb和一个日历erb等等。有没有办法明确告诉他们使用json er

我想创建一个.erb文件,作为返回JSON的许多小操作的输出。因此,路线类似于:

map.json 'contacts_json', :controller => 'contacts', :action => 'get_json' 
map.json 'cal_json', :controller => 'calendar', :action => 'get_json'
...
但这需要我创建一个联系人erb和一个日历erb等等。有没有办法明确告诉他们使用json erb?比如:

map.json 'contacts_json', :controller => 'contacts', :action => 'get_json', :view => 'layouts/json.html.erb'

否--您指定要在控制器操作中呈现的视图模板。

否--您指定要在控制器操作中呈现的视图模板。

我不确定它是否回答了您的问题,但使用render:text=>@foobar.to_json在某些情况下确实很神奇。

我不确定它是否回答了您的问题,但使用render:text=>@在某些情况下,foobar.to_json确实很神奇。

为什么不使用控制器操作所需的布局和视图呢。。render:template=>'shared/my.json.erb'为什么不直接使用控制器操作所需的布局和视图。。render:template=>'shared/my.json.erb'正是我想要的。谢谢,正是我想要的。非常感谢。