2011-03-30 68 views
1

我讓用戶從自動填充字段中選擇類型。該字段位於配置文件的編輯視圖中,因此我在配置文件模型中有accepts_nested_attributes_for :genres。另外,流派和個人資料有一個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 %> 
+0

的ID的隱藏的文本字段? 看看這個https://github.com/crowdint/rails3-jquery-autocomplete – Pravin 2011-03-30 04:37:47

+0

我使用這個自動完成:http://loopj.com/jquery-tokeninput/我已經預先填充數據庫和將腳本連接到文本字段。現在我想用選定的流派更新數據庫。 – 2011-03-30 04:45:17

回答

1

查看此網頁http://loopj.com/jquery-tokeninput/demo.html

在此頁面上點擊提交元素的警示標識,按我的理解。

下面的腳本在head做它

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

這種墊子幫助你.. 基本上沒有存儲您使用的是自動完成什麼選擇的元素

+0

ahh cool ...那麼我如何使用它來保存配置文件ID和選擇到連接表中的流派的ID之間的關聯? – 2011-03-30 06:00:23

+0

將自動完成的隱藏輸入命名爲'profile [gener_ids]'..並且它應該使用簡單的'profile.save'來執行這個技巧 – Pravin 2011-03-30 06:27:27

+0

我很困惑...什麼是隱藏輸入?你能提供一個代碼示例,並告訴我什麼控制器的方法? – 2011-03-30 06:31:11

相關問題