Search code examples
ruby-on-railsformsselected

FORM_TAG (Rails): how to add selected to an option?


I've a "form_tag" form in rails.

Now I've the problem, that I'm loading some options from my DB to a select tag.

<%= select("get", "object_id", Object.all.collect {|p| [p.name, p.id, {:'data-email' => p.email}] }) %>

I've the ID of the object I want to get selected in another variable @object.id

(normally, I solve that with collection_select. But that's not working together with "form_tag" as far as I can see - only with "form_for")


Solution

  • http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

    By default, post.person_id is the selected option. Specify :selected => value to use a different selection or :selected => nil to leave all options unselected.

    select("get", "object_id", Object.all.collect {|p| [p.name, p.id, {:'data-email' => p.email}] }, { :selected => @object.id })