2012-07-14 53 views
1

有一個基於Devise系統的用戶。
User_has_one:user_profile和user_profile_belongs_to:用戶
User_profile_has_one:語言,language_belongs_to:User_profile如何製作collection_select

在 'user_profiles' 表中,有知道一個叫 「LANGUAGE_ID」 列中的用戶在說什麼語言。
在'語言'表中,有一個名爲「名稱」的列。這可能像英文,西班牙文和語言類。

現在,我想補充的語言選擇我的色器件的編輯頁面

應該是這樣的下面。對?

<h2>Edit <%= resource_name.to_s.humanize %></h2> 

<% resource.build_user_profile if resource.user_profile.nil? %> 
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %> 
    <%= devise_error_messages! %> 

    <%= f.fields_for :user_profile do |profile_form| %> 

     <div><%= profile_form.label :nickname %><br /> 
     <%= profile_form.text_field :nickname %></div> 

     <div><%= profile_form.label :language_id %><br /> 
     <%= profile_form.collection_select(language_id, @languages, language_id, name_ja) %></div> 

    <% end %> 



    <div><%= f.label :email %><br /> 
    <%= f.email_field :email %></div> 

    <div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 
    <%= f.password_field :password %></div> 

    <div><%= f.label :password_confirmation %><br /> 
    <%= f.password_field :password_confirmation %></div> 

    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 
    <%= f.password_field :current_password %></div> 
    <br /> 
    <div><%= f.submit "Update" %></div> 
<% end %> 

<h3>Cancel my account</h3> 

<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p> 

<%= link_to "Back", :back %> 

更新!在registrations_controller.rb

def edit 
    @languages = Language.all 
    @countries = Country.all 
    @prefectures = Prefecture.all 
    @genders = Gender.all 
    end 

回答

1

編輯方法,假設你在控制器分配@languages,那麼你可能想是這樣的:

<%= profile_form.collection_select(:language_id, @languages, :id, :name) %> 

你想傳遞一個代表符號方法應該被調用,所以「:language_id」而不是「language_id」

+0

謝謝,我改成了這一點,但它仍說「未定義的方法'映射'爲零:NilClass'。 – MKK 2012-07-15 05:59:36

+0

我需要在我的控制器中寫些東西嗎?像@languages = ???或在視圖? – MKK 2012-07-15 06:02:26

+0

我在我的registrations_controller.rb中沒有編輯方法,因爲它每次調用設計。但我想弄清楚請檢查我的更新! – MKK 2012-07-15 06:09:33

0

在控制器操作中,您應該有 -

@languages = Language.find_by_sql("select distinct id, name from languages") 

,並在視圖

<%= collection_select :language, :id, @languages, :id, :name %> 

,並在jQuery中您可以使用訪問該變量 -

var languageId = $('select#language_id :selected').val(); 
+0

謝謝我試圖添加它們,除了jquery的東西。但是當我嘗試更新'未定義的方法'map for nil:NilClass'時,我仍然遇到這個錯誤: – MKK 2012-07-15 11:36:29