0

我正在使用rails-jquery-autocomplete,并未能实现重定向到选定的用户。Rails jquery atocomplete重定向到用户

这是我的形式:

<%= form_tag users_path, method: 'get', id: 'search-form' do %> 
     <%= autocomplete_field_tag :search, 'Find your friends...', home_autocomplete_user_username_path, 'min-length' => 1, class:'search_field', onfocus: 'if($(this).val()=="Find your friends..."){$(this).val("");};', onblur: 'if($(this).val()==""){$(this).val("Find your friends...");};' %> 
    <% end %> 

它重定向到users_path,它仅显示选定的用户 - 但我想重定向到他的节目页面。

其实我achived重定向到用户加入:

$('#search').bind('railsAutocomplete.select', function(event, data){ 
     $('#search-form').submit(); 
     window.location.href = '/users/' + data.item.id; 
    }); 

但我认为这不是最好的解决方案,并有更好的。预先感谢您的建议。

回答

1

下面的代码将在一个单独的标签打开展示页面为您提供:

$('#search').bind('railsAutocomplete.select', function(event, data){ 
    var url = "https://stackoverflow.com/users/" + data.item.id; 
    window.open(url, '_blank'); 
    window.focus(); 
}); 

让我知道如果任何问题。

+0

没错,但我想在不使用js的情况下得到这个效果 - 我只想使用搜索表单(如果可能的话)。 –

+0

Autocomplete为您提供了这种js方式来绑定您喜欢做的额外事情....我可以知道任何不使用js方式的特定原因吗? – SnehaT