简体   繁体   中英

rails form builder quick question

I need to create a select statment with name and id like this but no using a form builder for one of the fields.

<select name="link_list[links][][link_to_path]" id="link_list_links__link_to_path">

All other fields are created like this

f.collection_select "link_to_path", LinkList.all, :url, :name

but i need to do it creating the id and name manually like this :

collection_select "link_list[links][]", "link_to_path",LinkList.all, :url, :name

this doesnt work at the moment.

Anyone have an answer for this ?

thanks alot Rick

You want to use options_for_select :

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

options_for_select(container, selected = nil)

For example:

options_for_select([["Dollar", "$"], ["Kroner", "DKK"]])

That generates the following html:

 <option value="$">Dollar</option>\n<option value="DKK">Kroner</option>

So in your case you need an array of links with their name and url like:

links = [['Google', 'www.google.com'], 'Yahoo', 'www.yahoo.com']

Then you can use select_tag

<%= select_tag :link, options_for_select(links)%>

Then if you want to have a default link you just inject the array like this:

links.insert(0, ["Default", "www.twitter.com"])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM