开发者

rails form builder quick question

开发者 https://www.devze.com 2023-02-19 04:35 出处:网络
I need to create a select statment with name and id like this but no using a form builder for one of the fields.

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 a开发者_StackOverflow社区nswer 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"])
0

精彩评论

暂无评论...
验证码 换一张
取 消