我正在尝试将实例变量从我的rails应用程序传递到关联的coffeescript文件,但它目前似乎没有被解析。我错过了什么?
locations.js.coffee.erb
$.ajax
url: "/map_groups/<%= @id %>.json"
type: "get"
dataType: "json"
async: false
success: (response) ->
exports.response = response
locations_controller.rb
def index
@id = (params[:id]) ? params[:id] : 1
@locations = Location.all
end
但是这是控制台中显示的错误:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:3000/map_groups/.json
我可以做些什么来解析实例变量吗?
注意:
我知道该变量存在,因为它正在传递给 View 。
编辑:我正在尝试做什么
我的大部分数据都是通过 JSON 发送的,我创建了一个自定义路由,以便让 CoffeeScript 知道要提取哪些 json 数据:
get "/locations/map-group/:id", controller: :locations, action: :index, as: :map_group
如果您返回我的 Controller - 您将看到,如果用户访问普通的旧 /locations
,则 ID 默认为 1。否则,ID 就是 URL 中指定的任何内容。 Coffeescript 文件需要通过 AJAX 调用提取与该 ID 相关的数据。我如何告诉 CoffeeScript 该 ID 是什么?
最佳答案
我会做什么
如果可以避免的话,我强烈建议不要使用 Ruby 实例变量来生成 CoffeeScript。我建议使用这样的库来处理您正在考虑的用例:
https://github.com/railsware/js-routes
# Configuration above will create a nice javascript file with Routes object that has all the rails routes available:
Routes.users_path() // => "/users"
Routes.user_path(1) // => "/users/1"
Routes.user_path(1, {format: 'json'}) // => "/users/1.json"
Routes.new_user_project_path(1, {format: 'json'}) // => "/users/1/projects/new.json"
Routes.user_project_path(1,2, {q: 'hello', custom: true}) // => "/users/1/projects/2?q=hello&custom=true"
Routes.user_project_path(1,2, {hello: ['world', 'mars']}) // => "/users/1/projects/2?hello%5B%5D=world&hello%5B%5D=mars"
这加上 HTML5 data-*
标签将帮助您在 JavaScript 中传递所需的识别信息:
http://html5doctor.com/html5-custom-data-attributes/
例如:
<div id="my_awesome_location" data-location-id="<%= @location.id %>">...</div>
然后像这样加载你的ajax:
id = $("#my_awesome_location").data('location-id')
$.ajax
url: Routes.map_group_path(id) #=> "/map_groups/#{id}.json"
type: "get"
dataType: "json"
...
无论如何该怎么做
但是,如果您绝对必须在 CoffeeScript 中使用 ERB 样式标记,则可以使用 coffee.erb
文件扩展名来实现:
http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
2.3.3 JavaScript/CoffeeScript and ERB
If you add an erb extension to a JavaScript asset, making it something such as application.js.erb, then you can use the asset_path helper in your JavaScript code:
$('#logo').attr({
src: "<%= asset_path('logo.png') %>"
});
This writes the path to the particular asset being referenced.
Similarly, you can use the asset_path helper in CoffeeScript files with erb extension (e.g., application.js.coffee.erb):
$('#logo').attr src: "<%= asset_path('logo.png') %>"
为什么你可能不想那样做
我建议使用上述库而不是直接 ERB 的原因是 Controller / View 和 Assets 之间的紧密耦合。
这还意味着您必须先预加载整个应用程序,然后才能进行 Assets 编译,因此如果您尝试部署到像 Heroku 这样的 PaaS,您将会遇到麻烦。
https://devcenter.heroku.com/articles/rails-asset-pipeline
The app’s config vars are not available in the environment during the slug compilation process. Because the app must be loaded to run the assets:precompile task, any initialization code that requires existence of config vars should gracefully handle the nil case.
基本上,如果您稍微更改 Controller ,就有可能破坏您的 Assets 。最好将它们作为单独的单元,并引入一个中介层来处理您要解决的问题。
关于javascript - 是否可以解析 CoffeeScript 资源文件中的 Ruby 实例变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19261622/