如何设置在自动完成字段中选择的元素

发布于 2024-10-29 03:06:29 字数 1356 浏览 2 评论 0原文

我让用户从自动完成字段中选择类型。该字段位于配置文件的编辑视图中,因此我在配置文件模型中有 accepts_nested_attributes_for :genres 。此外,Genre 和 Profile 之间存在 has_and_belongs_to_many 关联。我的问题是,如何获取用户选择的类型并将其设置为该用户的个人资料?我需要什么样的控制器代码?代码会放入配置文件或流派控制器中吗?

我用它来自动完成: http://loopj.com/jquery-tokeninput/ 我'我们已经预先填充了流派表并将脚本连接到文本字段。现在,当用户键入时,自动完成功能会成功显示建议。现在我想用选定的流派更新数据库。该数据库是类型和配置文件之间的连接表。我该如何以及在哪里执行此操作?

总而言之,当我单击个人资料编辑视图中的按钮时,我想将个人资料 ID 与所选流派 ID 之间的关联保存到连接表中。现在我收到此错误:

ActiveRecord::UnknownAttributeError in ProfilesController#update

unknown attribute: genre
Rails.root: /rubyprograms/dreamstill

app/controllers/profiles_controller.rb:18:in `update'
app/controllers/profiles_controller.rb:17:in `update'

这是我的个人资料编辑视图:

<%= form_for(:profile, @profile, :url => {:controller => "profiles", :action => "update"}, :html => { :multipart => true, :method => :put }) do |f| %>
...
<%= f.fields_for :genre do |g| %>
 <div class="field">
      <%= g.label :name, "Genres" %><br />
      <%= g.text_field :name, :id => 'genre_field' %>
  </div>
<% end %>
...
<div class="action">
  <%= f.submit :profile, :value => "Update Profile" %>
</div>
<% end %>

I have users pick genres from an autocomplete field. This field is in the edit view of the profile, so I have accepts_nested_attributes_for :genres in the Profile model. Also, Genre and Profile have a has_and_belongs_to_many association with one another. My question is how do you take the genres that the user picks and set them for that user's profile? What kind of controller code would I need? Would the code go in the profiles or genres controller?

I'm using this for autocomplete: http://loopj.com/jquery-tokeninput/ I've already prepopulated the genres table and hooked up the script to the text field. Right now when a user types, the autocomplete successfully displays suggestions. Now I want to update the database with the selected genres. This database is a join table between genres and profiles. How and where do I do this?

To sum up, I want to save the association between the profile id and the ids of the genres selected into the join table when I click the button in the profile edit view. Right now I get this error:

ActiveRecord::UnknownAttributeError in ProfilesController#update

unknown attribute: genre
Rails.root: /rubyprograms/dreamstill

app/controllers/profiles_controller.rb:18:in `update'
app/controllers/profiles_controller.rb:17:in `update'

Here's my profile edit view:

<%= form_for(:profile, @profile, :url => {:controller => "profiles", :action => "update"}, :html => { :multipart => true, :method => :put }) do |f| %>
...
<%= f.fields_for :genre do |g| %>
 <div class="field">
      <%= g.label :name, "Genres" %><br />
      <%= g.text_field :name, :id => 'genre_field' %>
  </div>
<% end %>
...
<div class="action">
  <%= f.submit :profile, :value => "Update Profile" %>
</div>
<% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-11-05 03:06:30

检查此页面 http://loopj.com/jquery-tokeninput/demo.html

中根据我的理解,此页面单击提交元素的警报 ID。

head 下面的脚本可以做到这一点,

<script type="text/javascript">  
  $(document).ready(function() {  
    $("input[type=button]").click(function () {  
     alert("Would submit: " + $(this).siblings("input[type=text]").val());   
    });
   });
</script>

这个垫子可以帮助你..
基本上有一个隐藏的文本字段,用于存储所选元素的 id

Check this page http://loopj.com/jquery-tokeninput/demo.html

In this page clicking on submit alerts ids of the element, as per my understanding.

The script below in head does it

<script type="text/javascript">  
  $(document).ready(function() {  
    $("input[type=button]").click(function () {  
     alert("Would submit: " + $(this).siblings("input[type=text]").val());   
    });
   });
</script>

This mat help you..
Basically there is an hidden text field which stores ids of the selected elements

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文