【发布时间】:2012-11-20 08:52:28
【问题描述】:
我正在执行跨域 ajax 调用以从我的 rails 应用程序中获取一些数据。为此,我使用了一种我发现的很棒的方法,它就像一个魅力。然而,Rails 并没有呈现与它相关的关联。因为我确实想要这些信息,所以我尝试更改我的 format.js,但在格式化正确的输出时失败了。
这就是我现在的旅行展示方法(效果很好):
respond_to do |format|
format.html # show.html.erb
format.js { render_json @trip.to_json }
end
但我也想渲染@trip.triplocations。但我不知道怎么做。有谁知道这个吗?
以防万一,这是我发现的很棒的方法:
def render_json(json, options={})
callback, variable = params[:callback], params[:variable]
response = begin
if callback && variable
"var #{variable} = #{json};\n#{callback}(#{variable});"
elsif variable
"var #{variable} = #{json};"
elsif callback
"#{callback}(#{json});"
else
json
end
end
render({:content_type => :js, :text => response}.merge(options))
end
(道具转至:http://www.sitepoint.com/json-p-output-with-rails/)
提前致谢!
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 json associations