2017-08-31 63 views
1

我想在我的表單中使用選擇框。當我嘗試這種方式未定義的方法`合併'爲:名稱:符號

<%= s_form.select :experience_category_id, ExperienceCategory.all, :name,:id%> 

它給我的錯誤

未定義的方法合併爲:名稱:符號。

可能是因爲我必須使用簽名s_form.collection_select。但它不允許我多選。

當我嘗試這種方式

<%= s_form.select(:experience_category_id, ExperienceCategory.all {|p| [ p.name, p.id ] }, 
              { :prompt => "Please select"}, 
              { :multiple => true, :size => ExperienceCategory.all.count })%> 

Supplier_controller

def new 
    @user = User.new 
    @user.build_supplier 
    @user.supplier.build_natural_address 
    end 
def create 
    @user = User.new(user_params) 
    @user.role = "supplier" 

     if @user.save 
     UserMailer.account_activation(@user).deliver_now 
     flash[:notice] = "Please check your email to activate your account" 
     redirect_to root_url 
     else 
     render :new 
     end 

    end 

。 。 。

def user_params 
     params.require(:user).permit(:email, :first_name, :last_name, :password, :password_confirmation, supplier_attributes: [:id, :company_name, :insurance_document, :profile_image, :national,:experience_category_id, natural_address_attributes: [:addressable_id, :addressable_scope, :addressable_type, :address_line_1, :address_line_2, :address_line_3, :town, :county, :postcode]]) 
    end 

供應商創建我不能看到任何問題的服務器,但與出:experience_catagory_id。 它不會拋出錯誤,但:experience_category_id保持nil。有什麼可以解決的問題?我該如何解決這個問題?謝謝

+0

當我使用'.map'或'.collect'時,它返回一個枚舉值,當我使用'.pluck'時,它返回一個所有類型的數組,它們都很好,但仍然是'p'是'nil' – Nejweti

+0

什麼是軌道服務器日誌和你的控制器? –

+0

我更新了問題 – Nejweti

回答

1

this link是你的問題

好的樣品參考你的問題,如果你想選擇一個選擇

<%= s_form.collection_select :experience_category_id, ExperienceCategory.all, :id, :name, :prompt => 'Select' %> 

多選

<%= s_form.collection_select :experience_category_ids, ExperienceCategory.all, :id, :name, {}, {multiple: true'} %> 

在控制器請注意,尤其是在你的strong_parameters應該是

def user_params 
    params.require(:user).permit(
    :your_other_fields, 
    :experience_category_ids => [] 
end 
1
<%= f.select(:experience_category_id, ExperienceCategory.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %> 
+0

在這篇文章上的標籤不清楚,但......如果這是rails 3,那麼'ExperienceCategory.find(:all)'與'ExperienceCategory.all'完全相同(因此與OP當前提議相同)。如果這是rails 4,則'ExperienceCategory.find(:all)'將會失敗,因爲'#find'現在強制'to_i'爲基於整數的主鍵。無論哪種情況,如果沒有明確地添加該選項,'select'將不允許多個選項 – engineersmnky

0

您沒有使用正確的格式爲每軌HTML陣列有助於

使用本

<%= f.select(:product_category_id, product.find(:all).collect {|u| [u.name, u.id]}, :prompt => 'Select') %> 

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: 'None'}) 

在文檔 Rails helpers

相關問題