2016-02-01 50 views
0

我不確定如何解決下面的問題。任何幫助將非常感激簡單表單 - 將關聯集合設置爲默認ID

  • 我有whoes輪廓狀態,他們來自一個國家的country_category_id用戶:8
  • 當用戶填寫簽證申請表我想設置由國家選擇選項默認的用戶來自國家(country_category_id:8)在用戶的個人資料在_form.html.erb我設法與這個名字
  • 如說加入「值:current_user.firstname」

問題:可以一個建議如何做到這一點對於s simpleform做 協會選擇選項 - 設置國家選擇選項爲默認ID基於用戶國家ID

千恩萬謝

模式

create_table "users", force: true do |t| 
    t.string "firstname" 
    t.string "lastname" 
    t.integer "number" 
    t.string "email" 
    t.text  "language" 
    t.integer "category_country_id" 
    end 


    create_table "forms", force: true do |t| 
    t.string "firstname" 
    t.string "lastname" 
    t.integer "number" 
    t.string "email" 
    t.integer "category_country_id" 
    end 

    create_table "category_countries", force: true do |t| 
    t.string "name" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
    end 

模型

User belongs_to category_country 
Form belongs_to category_country 

CategoryCountry has_many users 
CategoryCountry has_many forms 

觀點:形式/ _form.h tml.erb

<%= f.input_field :firstname, value: current_user.firstname %> 
<%= f.association :category_country, collection: CategoryCountry.all, prompt: "select a category", label: "current country (you currently live in)", :value => current_user.category_country.name %> 

forms_controller.rb

def new 
    @user = current_user 
    @form = Form.new 
    end 

    def create 
    @form = Form.new(form_params) 
    @form.user_id = current_user.id 
    if @form.save 
     redirect_to application_submitted_path 
    else 
     redirect_to user_advert_path(advert.user, advert) 
    end 
    end 

回答

1

我還沒有在一段鐵軌的工作,但我試圖幫助。

您是否嘗試過將:selected => current_user.category_country_id

<%= f.association :category_country, collection: CategoryCountry.all, prompt: "select a category", label: "current country (you currently live in)", :value => current_user.category_country.name, :selected => current_user.category_country_id %> 
+1

你真棒!謝謝你soo '<%= f.association:category_country,collection:CategoryCountry.all,prompt:「select a category」,label:「current country(you live in)」,:value => current_user.category_country。名稱,:selected => current_user.category_country_id%>' – ARTLoe

相關問題