ruby-on-rails
  • ruby
  • ruby-on-rails-3
  • twitter-bootstrap
  • 2012-03-20 117 views 6 likes 
    6

    如何從引導整合預輸入這樣的:從Twitter的引導的形式使用預輸入(formtastic)

    <input type="text" class="span3" style="margin: 0 auto;" data-provide="typeahead" data-items="8" data-source='["University of Pennsylvania","Harvard","Yale","Princeton","Cornell","Brown","Columbia","Dartmouth"]'> 
    

    成標準形式是這樣的:

    <%= semantic_form_for(@education) do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field"> 
    <%= f.input :college, placeholder: "Update Education" %> 
    </div> 
    <%= f.submit "Submit", class: "btn btn-large btn-primary" %> 
    <% end %> 
    
    +0

    我沒有看過你的整個的問題!我相應地更新了我的答案。 – 2012-03-20 22:31:18

    回答

    6

    在您的控制器

    def index 
        @autocomplete_items = Model.all 
    end 
    

    在你看來,就像你有一個額外的選擇ID ...

    <% semantic_form_for(@education) do |f| %> 
        <%= render 'shared/error_messages', object: f.object %> 
        <div class="field"> 
        <%= f.input :college, placeholder: "Update Education", id: "auto_complete" %> 
        </div> 
        <%= f.submit "Submit", class: "btn btn-large btn-primary" %> 
    <% end %> 
    

    而且最重要的是,通過在控制器中定義的@autocomplete_items實例變量到您的視圖中的JavaScript變量:

    <%= javascript_tag "var autocomplete_items = #{ @autocomplete_items.to_json };" %> 
    

    這將序列化數據並使其可用JSON的事先鍵入的內容功能使用。

    至於事先鍵入的內容,僅僅是對象(@autocomplete_items)作爲JSON傳遞給Javascript,像這樣:

    <script type="text/javascript"> 
        jQuery(document).ready(function() { 
         $('#auto_complete').typeahead({source: autocomplete_items}); 
        }); 
    </script> 
    

    此外,還有一個Autocomplete gem for Rails 3這將直接與您的模型工作,而不是對象假冒到你的Javascript。文檔中甚至有一個Formtastic示例。

    編輯:它看起來像我沒有讀你的整個問題!不幸的是,HTML5數據屬性目前不支持Formtastic。然而,有一個separate branch確實包含對這些屬性的支持。

    除此之外,總是有隻是好醇」 HTML/ERB堅持動態特性是這樣的...

    <input type="text" class="span3" style="margin: 0 auto;" data-provide="typeahead" data-items="8" data-source='<%= @autocomplete_items.to_json %>'> 
    

    編輯2:我剛剛注意到兩兩件事。首先是我將JSON對象傳遞給一個Javascript變量的方式(請參閱示例)。其次,上述使用HTML5數據屬性的示例將而不是與Twitter的Typeahead插件一起使用,但它將與jQuery UI Autocomplete插件一起使用。

    +0

    也許這是因爲我對此有點新,但是我似乎無法得到您編寫的方法或自動完成寶石的工作方式。至於您的方法,我該在哪裏放置該JavaScript?我創建了一個名爲University的模型,它包含了大學名稱,並且我創建了一個名爲universities_controller的控制器,並將索引方法放入其中。儘管如此,鍵入功能仍然無法使用。請幫助? – 2012-03-21 15:58:09

    +0

    你得到的錯誤是什麼?是Ruby代碼還是Javascript的問題? – 2012-03-21 16:34:55

    +1

    我剛剛注意到,我將JSON傳入Javascript代碼的方式會產生問題。我已經提前解決了這個問題,使用'javascript_tag'助手,並使用ERB傳遞變量。 – 2012-03-21 17:10:27

    2

    我得到了它的工作,如:

    控制器

    @categories = Category.find(:all,:select=>'name').map(&:name) 
    

    ,並享有

    <input type="text" class="span3" style="margin: 0 auto;" data-provide="typeahead" data-items="8" data-source='<%= @categories.to_json %>'> 
    
    +0

    而不是'@categories = Category.find(:all,:select =>'name')。map(&:name)'你也可以做更短的'@categories = Category.uniq.pluck(:name)' 。 – rkallensee 2014-01-14 09:40:16

    +0

    這對於較小的數據集可以很好地工作,但是如果它們變大,處理所有記錄所需的時間將會妨礙打字頭快速工作。對於大數據集,您需要使用查詢,而不是返回塊上的所有json。 – 2015-05-19 16:22:45

    +0

    我同意低數據,這將很快。 對於大型設置,使用ajax將是更好的選擇。有多個typeahead ajax的例子。 – dbKooper 2015-05-20 14:15:34

    相關問題