适用于多个控制器的 JQuery UI 自动完成的 DRY 解决方案?
我有两个控制器,每个控制器都有一个自动完成文本框,可以按姓氏进行搜索。 autocomplete.json.erb 存储在其相应的视图中。
下面的自动完成代码片段对两个控制器都适用,
$( ".auto_complete" ).autocomplete({
minLength: 2,
source: 'autocomplete.json'
});
因为将生成以下请求:
http://localhost:3000/users/autocomplete.json?term=ab
http://localhost:3000/members/autocomplete.json?term=ab
但是,如果我使用 REST 路径,则 URL 末尾没有尾随“/”:
members_path: http://localhost:3000/users
users_path: http://localhost:3000/members
所以我必须这样做:
<%= link_to "Users", users_path+'/' %>
没有“ /' 最后,jquery-ui 将调用以下内容: 在
http://localhost:3000/autocomplete.json?term=ab
我看来,这不是一个干净的解决方案。 另一种方法是为每个控制器制作单独的自动完成代码和CSS类,以便可以将源属性分配给不同的控制器,例如:
source: '/users/autocomplete.json'
source: '/members/autocomplete.json'
这个问题有DRY解决方案吗?
谢谢。
I have two controllers, each has an autocomplete textbox to search by last name. autocomplete.json.erb is stored in its corresponding view.
This following autocomplete code snippet would work nice for both controllers
$( ".auto_complete" ).autocomplete({
minLength: 2,
source: 'autocomplete.json'
});
since these following requests will be generated:
http://localhost:3000/users/autocomplete.json?term=ab
http://localhost:3000/members/autocomplete.json?term=ab
however, if I use REST path, there is no trailing '/' at the end of URL:
members_path: http://localhost:3000/users
users_path: http://localhost:3000/members
So I have to do this:
<%= link_to "Users", users_path+'/' %>
without the '/' at the end, jquery-ui would invoke the following instead:
http://localhost:3000/autocomplete.json?term=ab
This does not seem to me as a clean solution.
Another way is to make a separate auto complete code and css class for each controller so that the source attribute can be assigned to a different controller, e.g:
source: '/users/autocomplete.json'
source: '/members/autocomplete.json'
Is there a DRY solution to this problem?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以询问当前 url 并将其用作前缀
You can ask the current url and use it as a prefix