rails mongoid autocomplete显示另一个字段的引用字段(rails mongoid autocomplete a reference field showing another

编程入门 行业动态 更新时间:2024-10-20 05:32:10
rails mongoid autocomplete显示另一个字段的引用字段(rails mongoid autocomplete a reference field showing another field)

最有可能提出类似的问题,但奇怪的是我无法使用这些关键字找到它...

假设你有一个带有字段的模型Actor :name 。 现在在其他地方有一个模特Movie ,其中有很多人has_many :actors 。 我正在尝试创建一个表单,我可以动态地将一些演员添加到电影中。

使用angularJS我会使用typeahead:

<input type="text" ng-model="actor_selected" placeholder="Add actor to movie" typeahead="actor as actor.name for actor in actors">

它基本上按actor名称显示和自动填充,但在内部将actor_id保存在$scope.actor_selected

如何在.erb文件中使用rails-autocomplete插件? 以下不能正常工作/做我想做的事:

<%= f.autocomplete_field :actor_id, movies_autocomplete_actor_name_path, class: "form-control" %>

Most likely similar questions have been asked, but weirdly I can't find it using those keywords...

Let's say you have a model Actor with a field :name. Now somewhere else there is a model Movie which has_many :actors. I'm trying to make a form where I can dynamically add some actors to a movie.

Using angularJS I would have used typeahead:

<input type="text" ng-model="actor_selected" placeholder="Add actor to movie" typeahead="actor as actor.name for actor in actors">

Which basically shows and autocompletes by actor name, but internally saves the actor_id in $scope.actor_selected

How can i do that using rails-autocomplete plugin in an .erb file? The following isn't working/doing what I want :

<%= f.autocomplete_field :actor_id, movies_autocomplete_actor_name_path, class: "form-control" %>

最满意答案

快速回答:使用:由gem autocomplete提供的id-element。

答案很长:即便如此,我找不到一种方法可以完全按照自己的意愿行事,所以我会重新实现后端部分。

我在这个问题的答案中描述了一切。

Fast answer: using :id-element provided by the gem autocomplete.

Long answer : Even with this, I couldn't find a way to do exactly what I wanted so I hed to reimplement the backend part.

I have described everything in the answer I gave in this question.

更多推荐

本文发布于:2023-08-04 13:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1416061.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   autocomplete   mongoid   rails   field

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!