在Rails应用程序中使用AJAX编写JS脚本的最佳实践?如何在脚本中包含URL?

时间:2022-12-06 09:25:29

I'd like to use a little bit of simple AJAX in my Rails application. The main problem I have is that inside a script I need to specify the URL where the request will go, which in most cases is a path to some Rails controller action. Anywhere else I'm advised not to write paths explicitly by hand (i.e. not do ugly stuff like '/my_resources/' + resource_id) but use helpers like my_resource_path instead.

我想在我的Rails应用程序中使用一些简单的AJAX。我遇到的主要问题是在脚本中我需要指定请求所在的URL,这在大多数情况下是一些Rails控制器操作的路径。在其他任何地方我都建议不要手动显式地写路径(即不要像'/ my_resources /'+ resource_id那样做丑陋的东西),而是使用像my_resource_path这样的帮助器。

But this approach doesn't work well with javascript, since these helpers don't work inside assets/javascripts. I can think of some ugly ways I can bypass the problem, currently I've implemented an extremely ugly workaround which is basically putting something like this inside my view:

但是这种方法与javascript不兼容,因为这些帮助程序在资产/ javascripts中不起作用。我可以想到一些可以绕过问题的丑陋方法,目前我已经实现了一个非常难看的解决方法,基本上在我的视图中放置了这样的东西:

<%= javascript_tag "onSubmitQuotePage('#{j escape_javascript(autocomplete_authors_url(''))}');"%>

But I can't imagine Rails developers didn't think of some prettier solution, some right way of doing AJAX.

但我无法想象Rails开发人员没有想到一些更漂亮的解决方案,一些正确的做AJAX的方法。

2 个解决方案

#1


2  

I can't say I like the way I do this so very happy to hear better ways of doing this.

我不能说我喜欢我这样做的方式,所以很高兴听到更好的方法。

But I either include the path in a data attribute on a relevant DOM element, or for some static routes I include a <script> block in the layout file that contains relevant paths.

但是我要么在相关DOM元素的数据属性中包含路径,要么对于某些静态路由,我在布局文件中包含一个包含相关路径的

<script>
(function() {
"use strict";
window.myapp || {};
window.myapp.new_order_path = '<%= new_order_path %>';
window.myapp.orders_path = '<%= orders_path %>';
...
}());
</script>

It's by no means a pretty solution, but the cases where I need a route in my JS are rather rare and that way I can then use myapp.new_order_path in my JS when I need it.

这绝不是一个漂亮的解决方案,但是我需要在我的JS中使用路由的情况相当罕见,这样我可以在需要时在我的JS中使用myapp.new_order_path。

#2


1  

Just to add my two cents here: you can use Rails URI helpers to generate some sort of URI templates. E.g., if you have this route defined:

只需在这里添加我的两分钱:您可以使用Rails URI帮助程序生成某种URI模板。例如,如果您定义了此路线:

edit_user GET /users/:id/edit(.:format) users#edit

edit_user GET /users/:id/edit(.:format)用户#edit

And you call it like edit_user_path(':user_id:'), it will return /users/:user_id:/edit. This way you can generate URI templates to be compiled by javascript. As @Tigraine said, you can include this in a data attribute of some top element, and use it from the client side.

你将它称为edit_user_path(':user_id:'),它将返回/ users /:user_id:/ edit。这样您就可以生成由javascript编译的URI模板。正如@Tigraine所说,你可以将它包含在某个*元素的数据属性中,并从客户端使用它。

It comes really in situations where you need to generate URIs for AJAX fetched resources: you just serve the resource id and let the javascript compile the URI from the template with a simple string.replace(':user_id:', user_id).

它实际上是在您需要为AJAX获取的资源生成URI的情况下:您只需提供资源ID并让javascript使用简单的string.replace(':user_id:',user_id)从模板编译URI。

#1


2  

I can't say I like the way I do this so very happy to hear better ways of doing this.

我不能说我喜欢我这样做的方式,所以很高兴听到更好的方法。

But I either include the path in a data attribute on a relevant DOM element, or for some static routes I include a <script> block in the layout file that contains relevant paths.

但是我要么在相关DOM元素的数据属性中包含路径,要么对于某些静态路由,我在布局文件中包含一个包含相关路径的

<script>
(function() {
"use strict";
window.myapp || {};
window.myapp.new_order_path = '<%= new_order_path %>';
window.myapp.orders_path = '<%= orders_path %>';
...
}());
</script>

It's by no means a pretty solution, but the cases where I need a route in my JS are rather rare and that way I can then use myapp.new_order_path in my JS when I need it.

这绝不是一个漂亮的解决方案,但是我需要在我的JS中使用路由的情况相当罕见,这样我可以在需要时在我的JS中使用myapp.new_order_path。

#2


1  

Just to add my two cents here: you can use Rails URI helpers to generate some sort of URI templates. E.g., if you have this route defined:

只需在这里添加我的两分钱:您可以使用Rails URI帮助程序生成某种URI模板。例如,如果您定义了此路线:

edit_user GET /users/:id/edit(.:format) users#edit

edit_user GET /users/:id/edit(.:format)用户#edit

And you call it like edit_user_path(':user_id:'), it will return /users/:user_id:/edit. This way you can generate URI templates to be compiled by javascript. As @Tigraine said, you can include this in a data attribute of some top element, and use it from the client side.

你将它称为edit_user_path(':user_id:'),它将返回/ users /:user_id:/ edit。这样您就可以生成由javascript编译的URI模板。正如@Tigraine所说,你可以将它包含在某个*元素的数据属性中,并从客户端使用它。

It comes really in situations where you need to generate URIs for AJAX fetched resources: you just serve the resource id and let the javascript compile the URI from the template with a simple string.replace(':user_id:', user_id).

它实际上是在您需要为AJAX获取的资源生成URI的情况下:您只需提供资源ID并让javascript使用简单的string.replace(':user_id:',user_id)从模板编译URI。