in Web & Tech

Create a select list from Active Record Object in Rails 3

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Learn More)

  • Related Content by Tag