2016-05-11 76 views
0

我是Ruby on Rails編程語言的新手。我想顯示一個動態下拉菜單。這裏是我的代碼:如何在Ruby on Rails中使用動態選項創建選擇標籤?

控制器

def property 
    @rental_types = RentalType.all 
end 

意見

<%= form_tag(@property, method: "post", class: 'form-horizontal form-label-left', multipart: true, novalidate:"") do %> 
    <%= select :property, :type_id, options_for_select(@rental_types.collect { |type| 
        [type.type_name.titleize, type.id] }, 1), {}, { id: 'countries_select', class: 'form-control col-md-7 col-xs-12' } %> 
<% end %> 

我的輸出應該

<select name="property[type_id]" class="form-control col-md-7 col-xs-12" id="countries_select"> 
    <option value="10">1 Bedroom Rentals</option> 
    <option value="11">2 Bedroom Rentals</option> 
    <option value="12">3 Bedroom Rentals</option> 
    <option value="13">4 Bedroom Rentals</option> 
</select> 

我想在我的視圖頁面動態下拉列表。但我得到undefined method 'type_id' for #<User:0x7aff460>錯誤消息。

請幫幫我。

回答

1

試試這個,

<%= select_tag 'property[type_id]', options_from_collection_for_select(@rental_types, 'id', 'name'), class: 'form-control col-md-7 col-xs-12', id: 'countries_select' %> 

欲瞭解更多詳細經過link

1

請試試這個

<%= select("property", "type_id", @rental_types.collect {|p| [ type.type_name.titleize, type.id] }, {prompt: 'Select Person'}) %> 
+0

提示無法正常工作。你可以請檢查'提示:'選擇人' – Chinmay235

相關問題