繁体   English   中英

如何在我的JavaScript中访问Rails路线/ URL?

[英]How can I access Rails Routes/URLs in my JavaScript?

我是Ruby on Rails的新手,很痛苦,所以在这里忍受。

因此,我为我的Web应用程序创建了以下URL:

HOMEPAGE (WHEN RUNNING PROJECT LOCALLY): localhost:3000
PRODUCTS PAGE: localhost:3000/products
TOYOTA PAGE: localhost:3000/products/toyota
HONDA PAGE: localhost:3000/products/honda
FORD PAGE: localhost:3000/products/ford
NISSAN PAGE: localhost:3000/products/nissan
CHEVROLET PAGE: localhost:3000/products/chevrolet
MERCEDES PAGE:  localhost:3000/products/mercedes
BMW PAGE: localhost:3000/products/bmw
HYUNDAI PAGE: localhost:3000/products/hyundai

这是我在config / routes.rb中创建这些URL的位置:

get 'products', to: 'static#products'
root to: 'static#home'

get 'products/toyota', to: 'static##products#toyota'
get 'products/honda', to: 'static#products#honda'
get 'products/ford', to: 'static#products#ford'
get 'products/nissan', to: 'static#products#nissan'
get 'products/chevrolet', to: 'static#products#chevrolet'
get 'products/mercedes', to: 'static#products#mercedes'
get 'products/bmw', to: 'static#products#bmw'
get 'products/hyundai', to: 'static#products#hyundai'

这是我的产品页面的JavaScript文件的片段:

    g.prototype.Ce = function(b,
        f) {

        var e = this;
        x.i.Mg(b, f);
        x.A("build/getModelOptions", {
            year: b,
            make: f,
            included_body_styles: x.options.included_body_styles
        }, function(b) {
            b = a.parseJSON(b);
            e.bg.render(b, function(b) {
                //$("#builder-make-selection .custom_dd_select a").text("Toyota");
                a("#builder_splash_models").html(b);
                a.each(a(".builder_body_types"), function(a, b) {
                    k(b);
                    console.log("b=" + JSON.stringify(b));
                    console.log("k(b)=" + JSON.stringify(k(b)));
                })
            })
        })
    };

基本上,我想做的是在JS的一行中

       make: f;

如果我转到“丰田”页面:我想让该行显示为:

      make: 'Toyota';

如果我转到本田页面,则希望该行显示为:

      make: 'Honda'

如果我转到福特页面,则要将该行显示为:

      make: 'Ford';

等等等等。 你明白了。 关于如何做到这一点的任何想法?

我认为不应考虑Rails来获取JavaScriptcurrent url 您可以使用以下JavaScript代码轻松获取current url

URL       -> document.location;
Path Name -> window.location.pathname;
Full URL  -> window.location.href;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM