Hello,

I found a way to make it work =).
The partial stays like this:

<dt><label for="record_state_">Estado</label></dt>
<dd>
<%= collection_select(nil, nil, State.find(:all), :id, :name,
    {:prompt => "Selecione..."},
    {:onchange => remote_function(:url  => {:action =>
"on_state_id_change"},
     :with => "'state_id='+value"),
     :class => "state-input",
     :id => "record_state_",
     :name => "record[state][id]"})
%>
</dd>

Then the method on the controller:
    def on_state_id_change
      render :update do |page|
        cities = State.find(params[:state_id]).cities
          page.replace 'select_city',
            "<div id='select_city'><dt><label
for='record_city_'>Cidade</label></dt><dd>" +
            collection_select(nil, nil, cities, :id, :name,
            {:prompt => "Selecione..."},
            {:class => "city-input",
             :id => "record_city_",
             :name => "record[city][id]"}) + "</dd></div>"
    end

Works for create/show, but when i edit a record it doesn't set the
proper values on the select,
my guess is that it happens because of the partials, can anyone help
me here?
What should be done?

Thanks,

Rafael


On 5 fev, 17:17, Rafael <[email protected]> wrote:
> I'm trying to do this based on this post 
> >http://pullmonkey.com/2008/3/30/dynamic-select-boxes-ruby-on-rails
> I created 2 partials
>
> _state_form_column.rhtml
>
> <label>Estado</label>
> <%= collection_select(nil, :state_id, states, :id, :name,
>                      {:prompt   => "Selecione um Estado"},
>                      {:onchange => "#{remote_function(:url  =>
> {:action => "update_cities"},
>                                                       :with =>
> "'state_id='+value")}"}) %>
>
> and
>
> _city_form_column.rhtml
> <label>Cidade</label>
> <%= collection_select(nil, :city_id, cities, :id, :name,
>                      {:prompt   => "Selecione uma cidade"}) %>
>
> And on the controller i have a method:
>   def update_cities
>     states = State.find(params[:state_id])
>     cities  = states.cities
>
>     render :update do |page|
>       page.replace_html 'cities', :partial =>
> 'state_form_column', :object => cities
>     end
>   end
>
> Webrick boots fine, but when i try to insert a new record i get this
> error on development.log
>
> ActionView::TemplateError (undefined local variable or method `states'
> for #<ActionView::Base:0xb6e2dd9c>) on line #2 of app/views/
> neighborhoods/_state_form_column.rhtml:
> 1: <label>Estado</label>
> 2: <%= collection_select(nil, :state_id, states, :id, :name,
> 3:                      {:prompt   => "Selecione um Estado"},
> 4:                      {:onchange => "#{remote_function(:url  =>
> {:action => "update_cities"},
> 5:                                                       :with =>
> "'state_id='+value")}"}) %>
>
> Looks pretty obvious, but i don't know how to fix this.
> Am i on the right way?
>
> Rafael
>
> On 5 fev, 09:51, Rafael <[email protected]> wrote:
>
> > Hello,
>
> > Thanks for the quick replies. I think in my case
> > AJAX will be more suitable, so Wes if you can
> > share that code i'll appreciate.
>
> > Rafael
>
> > On 3 fev, 23:56, Wes Gamble <[email protected]> wrote:
>
> > > I have done something like this using AJAX calls to generate the
> > > dependent select's options.
>
> > > If the Railscast doesn't help, let me know and I can send you some code.
>
> > > Wes
>
> > > Rafael wrote:
> > > > Hi,
>
> > > > How can i make a dynamic select? Suppose i have 3 models Country,
> > > > State and City.
> > > > City belongs_to State and State belongs_to Country.
> > > > Then i have a model that belongs_to the 3 models above.
> > > > What i want to do is when i select a country it updates the State
> > > > select with states from that country,
> > > > then i select a State and it updates the city select with cities from
> > > > that State.
> > > > Any ideas? examples?
>
> > > > Thanks,
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to