Forum: Ruby on Rails form_for and select ( => f.select)

Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-21 17:20
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
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-07-21 19:00
(Received via mailing list)
On Jul 21, 4:20 pm, Luca Scaljery <rails-mailing-l...@andreas-s.net>
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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-21 19:49
Frederick Cheung wrote:
> On Jul 21, 4:20�pm, Luca Scaljery <rails-mailing-l...@andreas-s.net>
> 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)
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-07-21 20:11
(Received via mailing list)
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?
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-21 22:40
Roy Pardee 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
D6434aa0b7b350f8c3ed0119d81b2ead?d=identicon&s=25 Roy Pardee (rpardee)
on 2008-07-21 23:23
(Received via mailing list)
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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-22 11:55
Roy Pardee 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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-22 12:01
Luca Scaljery wrote:
> Roy Pardee 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!!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-07-22 12:02
(Received via mailing list)
On Jul 22, 10:55 am, Luca Scaljery <rails-mailing-l...@andreas-s.net>
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
Ad745a3a6bdc249a7b34a0b6f5c8d5ec?d=identicon&s=25 Luca Scaljery (jeanluca)
on 2008-07-22 12:08
Frederick Cheung wrote:
> On Jul 22, 10:55�am, Luca Scaljery <rails-mailing-l...@andreas-s.net>
> 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 :)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.