0
我试着按照这个answer中的说明来渲染.js.erb文件中的json数据。但我得到这个错误:rails3:如何呈现json数据到.js视图?
ActionView::MissingTemplate (Missing template activities/json, application/json with {:handlers=>[:erb, :builder], :formats=>[:js, :html], :locale=>[:en, :en]}.
我有大量的数据发送给用户,因此我想发送为json比text/html。
这是我的控制器 'some_controller.rb':
def some_action
@hash = ...
@var = ...
.
.
respond_to do |format|
format.js { render :json }
end
end
这是我在视图/ some_controller/some_action.js.erb视图:
$("#some_div").html("<%=j render :partial => 'some_controller/another_action' %>");
这是我的another_action.html。 ERB:
<%= self.formats = ["html"] %>
Var1 = <%= @var1 %>
Hash1 = <%= @hash1.inspect %>
是在上述错误的链接的答案吗?我们不能将json数据发送到js.erb文件吗?我试图使用.to_json转换变量和哈希值。例如:
@hash1 = someMethod(someParams).to_json
但@ HASH1被渲染为我最后html.erb一个字符串,它不能被解析,当然!有没有我做错了或错过了?
另外,没有教程可以在线获取json在rails中的渲染,有人可以提供一些不错的链接来学习和json和rails吗?非常感谢帮助。提前致谢。
谢谢 的pR
我做了同样的事情,但这次我得到了406个不可接受的错误。我的网址:domain/controller/index。请帮忙 – Ari53nN3o 2012-02-04 09:21:01