mercredi 11 mai 2016

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.

Aucun commentaire:

Enregistrer un commentaire