Form_for and select ( => f.select)


#1

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


#2

On Jul 21, 4:20 pm, Luca S. 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


#3

Frederick C. wrote:

On Jul 21, 4:20�pm, Luca S. 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)


#4

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?


#5

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


#6

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:inform_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:inhaml_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:inrender’
/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


#7

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!!


#8

On Jul 22, 10:55 am, Luca S. 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


#9

Frederick C. wrote:

On Jul 22, 10:55�am, Luca S. 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 :slight_smile:


#10

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