# Form\_for and select ( =\> f.select)

**URL:** <https://www.ruby-forum.com/t/form-for-and-select-f-select/143660>\
**Category:** Rails\
**Created:** [July 21, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660 "2008-07-21T17:20:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 5:20pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/1 "2008-07-21T17:20:24Z")

</div>

Hi All

I’m trying to combine the from\_for and select helpers

Right now I have  
form\_for :service, :url =\> … do |f|

select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
{:prompt =\> “opt1”} )

…  
end

What I want to do is something like

…  
f.select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
{:prompt =\> “opt1”} )  
…

(this doesn’t work)  
I’ve tried many things but I cannot get this to work, any suggestions ?

thnx a lot  
LuCa

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/2 "2008-07-21T19:00:36Z")

</div>

On Jul 21, 4:20 pm, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
> {:prompt =\> “opt1”} )
> 
> …  
> end
> 
> What I want to do is something like
> 
> …  
> f.select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },

You shouldn’t need that first parameter (which effectively provided by  
the form\_for)

Fred

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/3 "2008-07-21T19:49:32Z")

</div>

Frederick C. wrote:

> On Jul 21, 4:20ï¿½pm, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > ï¿½ select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
> > {:prompt =\> “opt1”} )
> > 
> > ï¿½ …  
> > ï¿½ end
> > 
> > What I want to do is something like
> > 
> > ï¿½ …  
> > ï¿½ f.select(“abc”, “xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },
> 
> You shouldn’t need that first parameter (which effectively provided by  
> the form\_for)
> 
> Fred

I tried

f.select(“xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
{:prompt =\> “opt1”} )

but doesn’t work (error)

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/4 "2008-07-21T20:11:30Z")

</div>

What version of rails?

Show us the whole form\_for call please.

Tell us about the error–what’s the message, and which line causes it?

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/5 "2008-07-21T23:23:23Z")

</div>

What do you want the user to see in the drop-down? What do you want  
stored in the xyz\_id field of your @xyz object? I’m having a hard time  
understanding the .collect call in there.

Does this run without error?

f.select(“xyz\_id”, @my\_arr, {:prompt =\> “opt1”})

Cheers,

-Roy

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 21, 2008, 10:40pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/6 "2008-07-21T22:40:56Z")

</div>

Roy P. wrote:

> What version of rails?
> 
> Show us the whole form\_for call please.
> 
> Tell us about the error–what’s the message, and which line causes it?

I’m using rails 2.0.2  
In the view(haml):

- form\_for :xyz, :url =\> my\_url(params[:my\_id],@vals), :html =\> {:method  
=\> ‘put’, :multipart =\> true} do |f|  
f.select(“xyz\_id”, @my\_arr.collect {|item| [item, “bla”] },  
end

The ERROR:

undefined method `merge’ for [[“A”, “bla”], [“B”, “bla”], [“C”,  
“bla”]]:Array

Trace:

vendor/rails/actionpack/lib/action\_view/helpers/form\_options\_helper.rb:420:in  
`select' app/views/services/_abc.haml:34:in`\_run\_template\_47app47views47xyz47\_abc46haml’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action\_view\_mods.rb:72:in  
`call' /Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action_view_mods.rb:72:in`form\_for’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers.rb:336:in `call' /Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers.rb:336:in`haml\_bind\_proc’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action\_view\_mods.rb:76:in  
`form_for' app/views/xyz/_abc.haml:1:in`\_run\_template\_47app47views47xyz47\_abc46haml’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action\_view\_mods.rb:7:in  
`render' /Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action_view_mods.rb:7:in`render’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/haml/helpers/action\_view\_mods.rb:7:in  
`render' app/views/xyz/abc.haml:1:in`\_run\_template\_47app47views47xyz47abc46haml’  
/Library/Ruby/Gems/1.8/gems/haml-1.8.2/lib/sass/plugin/rails.rb:19:in  
`process’

And in the controller/action I have

@my\_arr = %w(A B C)

Hopefully it makes it makes sense

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 22, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/7 "2008-07-22T12:01:02Z")

</div>

Luca S. wrote:

> Roy P. wrote:
> 
> > What do you want the user to see in the drop-down? What do you want  
> > stored in the xyz\_id field of your @xyz object? I’m having a hard time  
> > understanding the .collect call in there.
> > 
> > Does this run without error?
> > 
> > f.select(“xyz\_id”, @my\_arr, {:prompt =\> “opt1”})
> > 
> > Cheers,
> > 
> > -Roy
> 
> if I do that I get the following error
> 
> undefined method `xyz\_id’ for #\<Xyz:0x4304cfc

The problem is solved.  
The problem occured due to the first 2 parameters. For example, this  
doesn’t work  
select(“abc”, “abc\_id” …)

but this does  
select(“abc”, “def\_id” … )

so now it works for f.select too!

thnx a lot for the help!!

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 22, 2008, 12:02pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/8 "2008-07-22T12:02:31Z")

</div>

On Jul 22, 10:55 am, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > -Roy
> 
> if I do that I get the following error
> 
> undefined method `xyz\_id’ for #\<Xyz:0x4304cfc

Then something is fundamentally scewed: why are you trying to edit  
xyz\_id if Xyz doesn’t have xyz\_id column? (also this would probably be  
a lot easier to follow with ‘real’ class/column names)

Fred

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 22, 2008, 12:08pm UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/9 "2008-07-22T12:08:38Z")

</div>

Frederick C. wrote:

> On Jul 22, 10:55ï¿½am, Luca S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > > -Roy
> > 
> > if I do that I get the following error
> > 
> > undefined method `xyz\_id’ for #\<Xyz:0x4304cfc
> 
> Then something is fundamentally scewed: why are you trying to edit  
> xyz\_id if Xyz doesn’t have xyz\_id column? (also this would probably be  
> a lot easier to follow with ‘real’ class/column names)
> 
> Fred  
> you’re right, next time I’ll do that, I thought things would be easier  
> with xyz and abc 🙂

---

<div class="post-metadata">

**Author:** ![jeanluca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/74df32/32.png) [@jeanluca](https://www.ruby-forum.com/u/jeanluca)\
**Post date:** [July 22, 2008, 11:55am UTC](https://www.ruby-forum.com/t/form-for-and-select-f-select/143660/10 "2008-07-22T11:55:04Z")

</div>

Roy P. wrote:

> What do you want the user to see in the drop-down? What do you want  
> stored in the xyz\_id field of your @xyz object? I’m having a hard time  
> understanding the .collect call in there.
> 
> Does this run without error?
> 
> f.select(“xyz\_id”, @my\_arr, {:prompt =\> “opt1”})
> 
> Cheers,
> 
> -Roy

if I do that I get the following error

undefined method `xyz\_id’ for #\<Xyz:0x4304cfc
