本文介绍了扶手:我如何自动完成搜索的名称,但保存ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个视频http://railscasts.com/episodes/102-auto-complete-association-revised设置自动完成搜索输入在我的应用程序的形式。 (视频可能是为会员,所以我会后我的code为好。本质上,它在下拉您键入搜索数据库(名称),并自动完成的一列。这一切工作正常不过,我ð等的形式做的就是提交给相关的名称标识,而不是名字本身。

我假设世界上没有简单的方法来做到这一点,在短短的观点。任何帮助将是巨大的。低于code,让我知道是否有任何其他code将是有益的。

感谢

控制器:

 高清game_name
  game.try(:名称)
结束

高清game_name =(名称)
  self.game = Game.find_by_name(名)如果名称。present?
结束
 

咖啡:

  jQuery的 - >
 $('#play_game_name)。自动完成
  来源:$('#play_game_name)的数据(自动完成源)。
 

在该视图:

 <%= f.label:game_name,搜索游戏%>
   <%= f.text_field:game_name,:类=> 传销MTM的数据:{autocomplete_source:Game.order(:名称).MAP(安培;:名)}%GT;
 

解决方案

在除了做的@LukasSvoboda建议,你也可以覆盖选择的回调,当你从下拉了一个项目被触发。在回调中,可以将文本字段设置(这并不需要提交)到所选项目的标签,并设置游戏ID(这确实需要提交)的隐藏字段的值价值所选择的项目。

在咖啡:

  jQuery的 - >
  $('#play_game_name)。自动完成
    来源:$('#play_game_name)的数据(自动完成源)。
    选择:(事件,UI) - >

    #需要从填补prevent自动完成
    #与值,而不是在标签
    事件。preventDefault()

    $(本).VAL ui.item.label
    $('#game_id)。VAL ui.item.value
 

在标记:

 <%= text_field_tag​​零,零,:ID => play_game_name',:类=> 传销MTM的数据:{autocomplete_source:Game.order(:名称).MAP {| T | {:标签=> t.name,:价值=> t.id}}%GT;
<%= f.hidden_​​field:game_id,ID:game_id%> #裁缝模型
 

I've used this video http://railscasts.com/episodes/102-auto-complete-association-revised to set up an autocomplete search input in a form for my app. (The video may be for members only so I'll post my code as well. Essentially it searches a column of the DB (name) and autocompletes in a dropdown as you type. This all works fine however, what I'd like the form to do is submit the ID that correlates to the name, not the name itself.

I'm assuming theres no simple way to do this in just the view. Any help would be great. Code below, let me know if any other code would be helpful.

Thanks

Controller:

def game_name
  game.try(:name)
end

def game_name=(name)
  self.game = Game.find_by_name(name) if name.present?
end

Coffee:

jQuery ->
 $('#play_game_name').autocomplete
  source: $('#play_game_name').data('autocomplete-source')

In the view:

   <%= f.label :game_name, "Search for a game" %>
   <%= f.text_field :game_name, :class => "mlm mtm", data: {autocomplete_source: Game.order(:name).map(&:name)} %>
解决方案

In addition to doing as @LukasSvoboda suggested, you can also override the select event callback, which gets triggered when you select an item from the dropdown. In the callback, you can set the text field (which doesn't need to be submitted) to the "label" of the selected item and set the value of a hidden field of the game id (which does need to be submitted) to the "value" of the selected item.

In coffee:

jQuery ->
  $('#play_game_name').autocomplete
    source: $('#play_game_name').data('autocomplete-source')
    select: (event, ui) ->

    # necessary to prevent autocomplete from filling in
    # with the value instead of the label
    event.preventDefault()

    $(this).val ui.item.label
    $('#game_id').val ui.item.value

In markup:

<%= text_field_tag nil, nil, :id => 'play_game_name', :class => "mlm mtm", data: {autocomplete_source: Game.order(:name).map { |t| { :label => t.name, :value => t.id } } %>
<%= f.hidden_field :game_id, id: 'game_id' %> # tailor to your model

这篇关于扶手:我如何自动完成搜索的名称,但保存ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 15:12