Chinmay235
Chinmay235

Reputation: 3414

How to create a select tag with dynamic options in Ruby on Rails?

I am new in Ruby on Rails programming language. I want to show one dynamic dropdown. Here is my code:

controller

def property
   @rental_types = RentalType.all
end

views

<%= 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 %>

My output should be

<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>

I want to dynamic dropdown in my view page. But I am getting undefined method 'type_id' for #<User:0x7aff460> error message.

Please help me.

Upvotes: 0

Views: 3062

Answers (2)

Amol Udage
Amol Udage

Reputation: 3075

Please try this

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

Upvotes: 1

Ganesh Sagare
Ganesh Sagare

Reputation: 375

Try this,

<%= 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' %>

For more detail go through link

Upvotes: 1

Related Questions