Forum: Ruby on Rails Problem with dynamic selects

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bradley H. (Guest)
on 2009-05-01 13:19
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]
  Make Columns (13.0ms)   SHOW FIELDS FROM `makes`
  Make Load (1.0ms)   SELECT * FROM `makes` WHERE
(`makes`.`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/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:in `render'
    app/controllers/site_controller.rb:46:in `__instance_exec0'
    app/controllers/site_controller.rb:45:in `update_models'
    haml (2.0.9) lib/sass/plugin/rails.rb:19:in `process'
    c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
    c:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
    c:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
    -e:2:in `load'
    -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)
  SQL (1.0ms)   SET NAMES 'utf8'
  SQL (0.0ms)   SET SQL_AUTO_IS_NULL=0


Any suggestions?  Thanks!
gaurav v bagga (Guest)
on 2009-05-01 14:14
(Received via mailing list)
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>
Bradley H. (Guest)
on 2009-05-02 00:09
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.
Bradley H. (Guest)
on 2009-05-02 00:51
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.
This topic is locked and can not be replied to.