如何在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';

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

I'm painfully new to Ruby on Rails, so bear with me here.

So, I just created the following URLs for my web application:

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

Here's my config/routes.rb where I'm creating these URLs:

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'

And here's a snippet of the JavaScript file for my products page:

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))); }) }) }) };

Basically, what I want to do is, in the JS, in the line that reads

make: f;

if I go to the Toyota page: I want to make that line to read:

make: 'Toyota';

and if I go to the Honda page, I want that line to read:

make: 'Honda'

and if I go to the Ford page, I want to make that line read:

make: 'Ford';

And so on and so forth. You get the idea. Any ideas on how to do that?

最满意答案

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

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

I think Rails should not be considered to get the current url in JavaScript. You can get current url using following JavaScript code easily

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

更多推荐