erb 不会在 CoffeeScript 文件中呈现

发布于 2024-12-13 02:50:21 字数 2353 浏览 2 评论 0原文

我无法弄清楚为什么我们的 ruby​​ 代码没有在咖啡脚本中呈现。最终,我们致力于在 Google 地图视图中绘制存储在 Rails 模型中的一系列标记。因为我们在循环方面遇到了麻烦,所以我们尝试获取一个测试变量来在警报中呈现;它不起作用。弹出警报,但它只显示 Hello,而不是 Hello 5 (或 Hello "5" ?)

这是我们的咖啡脚本文件(位置.coffee.erb)

$ ->
    alert "Hello <%= @test %>"
    latlng = new google.maps.LatLng(43.0, -107.0);
    myOptions =
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    point = new google.maps.LatLng(43.0, -107.0);
    marker = new google.maps.Marker({
        position: point,
        map: map,
        title: "Test",
    });

@test 在 #map 中定义,位于locations_controller.rb 的底部:

class LocationsController < ApplicationController

    def show
        @traveling_party = TravelingParty.find_by_id(session[:party])
        @location = Location.where("position >= #{@traveling_party.position}").order("position ASC").first
    end

    def move
        f = params[:traveling_party]

        @traveling_party = TravelingParty.find(f["id"])
        @traveling_party.speed = f["speed"].to_i
        @traveling_party.ration = f["ration"].to_i

        @location = Location.where("position > #{@traveling_party.position}").order("position ASC").first

        @traveling_party.position += @traveling_party.speed
        if @traveling_party.position > @location.position 
            @traveling_party.position = @location.position
        end

        food_eaten = @traveling_party.ration * @traveling_party.people
        Item.where({:trader_id => @traveling_party.id, :type => "Food"}).limit(food_eaten).destroy_all()

        if @traveling_party.save()
            flash[:notice] = "Successfully updated traveling party."
            redirect_to '/play/'
        else
            flash[:error] = "Transaction could not be completed."
            redirect_to '/play/'
        end
    end

    def map
        @traveling_party = TravelingParty.find_by_id(session[:party])
        @locations = Location.all
        @test = "5"
    end

end

对我来说,奇怪的是 @test 在我们的 map.html.erb 视图中正确渲染。正如预期的那样,5 打印在地图上方:

<% @locations.each do |loc| %>

<% end %>

<div id="map_canvas">Random</div>

I'm having trouble figuring out why our ruby code isn't rendering in coffeescript. Ultimately we're working towards plotting a series of markers stored in a Rails model in a Google Maps view. Because we had trouble with a loop, we tried to get a test variable to render in an alert; it's not working. The alert pops up, but it says only Hello, not Hello 5 (or Hello "5" ?)

This is our coffeescript file (locations.coffee.erb)

$ ->
    alert "Hello <%= @test %>"
    latlng = new google.maps.LatLng(43.0, -107.0);
    myOptions =
        zoom: 5,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    point = new google.maps.LatLng(43.0, -107.0);
    marker = new google.maps.Marker({
        position: point,
        map: map,
        title: "Test",
    });

@test is defined in #map, at the bottom of locations_controller.rb:

class LocationsController < ApplicationController

    def show
        @traveling_party = TravelingParty.find_by_id(session[:party])
        @location = Location.where("position >= #{@traveling_party.position}").order("position ASC").first
    end

    def move
        f = params[:traveling_party]

        @traveling_party = TravelingParty.find(f["id"])
        @traveling_party.speed = f["speed"].to_i
        @traveling_party.ration = f["ration"].to_i

        @location = Location.where("position > #{@traveling_party.position}").order("position ASC").first

        @traveling_party.position += @traveling_party.speed
        if @traveling_party.position > @location.position 
            @traveling_party.position = @location.position
        end

        food_eaten = @traveling_party.ration * @traveling_party.people
        Item.where({:trader_id => @traveling_party.id, :type => "Food"}).limit(food_eaten).destroy_all()

        if @traveling_party.save()
            flash[:notice] = "Successfully updated traveling party."
            redirect_to '/play/'
        else
            flash[:error] = "Transaction could not be completed."
            redirect_to '/play/'
        end
    end

    def map
        @traveling_party = TravelingParty.find_by_id(session[:party])
        @locations = Location.all
        @test = "5"
    end

end

What seems strange to me is that @test is rendering properly in our map.html.erb view. As expected, 5 is printed above the map:

<% @locations.each do |loc| %>

<% end %>

<div id="map_canvas">Random</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

四叶草在未来唯美盛开 2024-12-20 02:50:22

该文件确实通过 ERB 运行,但仅运行一次 - 当应用程序预编译资产时。这不是你可以轻易改变的事情,你可能不应该尝试。

您可以做的是通过 AJAX JSON 请求获取您想要单独填充的实际数据,或者将其作为 Javascript 嵌入到您的页面模板中。

静态(CSS + JS)文件的 ERB 编译通常用于放入不随每个请求而改变的环境特定数据。

The file is indeed being run through ERB, but only once - when the application is precompiling assets. And this isn't something you can change very easily, and you probably shouldn't try.

What you could do instead is fetch the actual data that you want populated separately via an AJAX JSON request or maybe embed it as Javascript in your page template.

The ERB compilation of static (CSS + JS) files is normally used to put in environment specific data that doesn't change with each request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文