Problem with dynamic selects


#1

I’m trying to create dynamic select menus for my application. I have a
partial that is rendered by an action in my site controller after an
:onchange event is triggered. I’ll post the three pieces of code and
the error I keep getting.

_models.html.haml

  • if models
    = collection_select :model_id, models, :id, :name, {:prompt => “Select
    Model”}

site_controller.rb
def update_models

update models in dynamic select boxes

make = Make.find(params[:make_id])
models = make.models

render :update do |page|
page.replace_html ‘models’, :partial => ‘models’, :object => models
end
end

browse.html.haml
.form_row
= form.collection_select :make_id, Make.find(:all), :id, :name,
{:prompt => “Select Makes”}, {:onchange => “#{remote_function(:url =>
{:action => “update_models”}, :with => “‘make_id=’+value”)}”}
.form_row#models
= render :partial => ‘models’, :object => @models

And from my log:

Processing SiteController#update_models (for 127.0.0.1 at 2009-05-01
04:41:13) [POST]
Parameters:
{“authenticity_token”=>“aOem8MBQboKI/e+KdHjNKgm2DvIQYFCyk14o6TtCj/w=”,
“make_id”=>“1”}
Asked for a remote server ? true, ENV[“FERRET_USE_LOCAL_INDEX”] is nil,
looks like we are not the server
Will use local index.
using index in C:/Users/Bradley/Documents/Aptana
Studio/GreenYard_003/index/development/make
default field list: [:name]
e[4;35;1mMake Columns (13.0ms)e[0m e[0mSHOW FIELDS FROM makese[0m
e[4;36;1mMake Load (1.0ms)e[0m e[0;1mSELECT * FROM makes WHERE
(makes.id = 1) e[0m
Asked for a remote server ? true, ENV[“FERRET_USE_LOCAL_INDEX”] is nil,
looks like we are not the server
Will use local index.
using index in C:/Users/Bradley/Documents/Aptana
Studio/GreenYard_003/index/development/model
default field list: [:name]

ActionView::TemplateError (undefined method `map’ for :id:Symbol) on
line #2 of app/views/site/_models.html.haml:
1: - if @models
2: = collection_select :model_id, @models.name, :id, :name, {:prompt
=> “Select Model”}

app/views/site/_models.html.haml:2:in

_run_haml_app47views47site47_models46html46haml_locals_models_object' haml (2.0.9) lib/haml/helpers/action_view_mods.rb:14:inrender’
app/controllers/site_controller.rb:46:in __instance_exec0' app/controllers/site_controller.rb:45:inupdate_models’
haml (2.0.9) lib/sass/plugin/rails.rb:19:in process' c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:inservice’
c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in run' c:/ruby/lib/ruby/1.8/webrick/server.rb:173:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:162:instart_thread’
c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:92:ineach’
c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in start' c:/ruby/lib/ruby/1.8/webrick/server.rb:23:instart’
c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in start' -e:2:inload’
-e:2

Rendered rescues/_trace (193.0ms)
Rendered rescues/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)
es/_request_and_response (1.0ms)
Rendering rescues/layout (internal_server_error)
e[4;35;1mSQL (1.0ms)e[0m e[0mSET NAMES 'utf8’e[0m
e[4;36;1mSQL (0.0ms)e[0m e[0;1mSET SQL_AUTO_IS_NULL=0e[0m

Any suggestions? Thanks!


#2

Hi,
can you verify parameters to collection_select. Are they proper?

Regards,
gaurav

On May 1, 2:19 pm, Bradley H. removed_email_address@domain.invalid


#3

Okay I solved that, partially. I needed to pass :locals => {:form =>
form} from my view when first rendering the partial. The problem is,
though, that on the :onchange event, the partial is rendered through the
controller, so I can’t pass :locals from there. So after the :onchange
event, I get the same error with the form.collection_select throwing a
“Wrong number of arguments 0 for 1” error.


#4

gaurav v bagga wrote:

Hi,
can you verify parameters to collection_select. Are they proper?

Regards,
gaurav

On May 1, 2:19�pm, Bradley H. removed_email_address@domain.invalid

After doing some testing, it would appear Rails is just having a major
problem with my partial. The parameters are fine, but something about
having that code in a partial is throwing it off. When I replace the
current stuff with the exact same code that I have in my
browse.html.haml layout, it messes up and gives me weird errors about
arguments…

wrong number of arguments (0 for 1)

Extracted source (around line #4):

1: - if @models
2: = form.collection_select :model_id, @models, :id, :name, :prompt =>
“Select Model”
3: - else
4: = form.collection_select :model_id, Model.find(:all), :id, :name,
{:prompt => “Select Model”}

If I leave “form.” off, I get the “undefined method :map for :id:Symbol
error” still.