Create a select list from Active Record Object in Rails 3

Nov 20, 2012 21:27 · 49 words · 1 minute read rails

Build the select form in your view…

<% @categories = Category.find(:all, :order=>"title") %>
<%= collection_select(:service, :category_id, @categories, :id, :title) %>

And access the selected value from the controller (inside the create and update methods):

@service.category_id = params[:service][:category_id]

Of course category_id needs to be made accessible in the services model.